Posts

Showing posts with the label bash

Replace character string between new lines

Image
Clash Royale CLAN TAG #URR8PPP Replace character string between new lines I am trying to edit a file such that strings flanked by new lines are removed. My file looks like: ENSG00000000460_chr1.dat varX data data data data data varX data data data data data varX data data data data data ENSG00000005801_chr11.dat ENSG00000006007_chr16.dat ENSG00000006607_chr2.dat varX data data data data data varX data data data data data ENSG00000010219_chr12.dat ENSG00000011052_chr17.dat The output I am trying to get would delete lines which are flanked by new lines (and delete the new lines) resulting in output which looks like: ENSG00000000460_chr1.dat varX data data data data data varX data data data data data varX data data data data data ENSG00000006607_chr2.dat varX data data data data data varX data data data data data I have tried various ideas in sed, but it either returns: sed 's/[na-zA-Z0-9n]//g' file.txt | head _. . .- . . . . . . .-...

Detect availability of GTK3 in shell script

Image
Clash Royale CLAN TAG #URR8PPP Detect availability of GTK3 in shell script We want to avoid the user to make changes to our application start script of only GTK2 is available, but not GTK3. What is the preferred way to detect the availability of GTK3? By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

bash - output command that can install listed npm global packages

Image
Clash Royale CLAN TAG #URR8PPP bash - output command that can install listed npm global packages I know npm ls -g --depth=0 will produce output npm ls -g --depth=0 C:UsersleonAppDataRoamingnpm +-- create-react-app@1.5.2 +-- eslint@5.2.0 +-- prettier@1.13.7 `-- serve@9.4.0 I'm looking for a bash command that can output the following based on the above output: npm i -g create-react-app npm i -g eslint npm i -g prettier npm i -g serve 2 Answers 2 you can use the sed tool to handle this sed npm ls -g --depth=0 | sed -nE 's/^W+/npm i -g /p' It's really close, but how to produce them without the trailing @version – Leon Du 23 hours ago @version Add s/@.*//; just after the opening single quote. ...

How to set max size for a specific file in linux

Image
Clash Royale CLAN TAG #URR8PPP How to set max size for a specific file in linux I have an application which will write to a particular log file until the user session is going on. What i am looking for is to put a max cap on the size of a log file so it does not grow beyond a particular size, 2 scenarios which will be useful are Any utility which keeps an eye on the log file and as soon as it reaches the max size start truncating the file content from the start so the application can keep appending the content at the end. Any utility by which while creating the file i can specify the max size of that file and when file reaches that maxsize it should simply not grow beyond that point. What i don't want is man logrotate – Cyrus Dec 17 '17 at 10:46 man logrotate @NiallCosgrove How to put a cap on the size o...

BASH case not catching empty string

Image
Clash Royale CLAN TAG #URR8PPP BASH case not catching empty string I want to detect if either no arguments or an invalid argument is passed and print a help message. A separate check for an empty argument is possible, but not so elegant. My bash script looks like this: COMMAND="$1" shift case "$COMMAND" in loop) loop_ ;; ... *) echo $"Usage: $0 {loop|...}" exit 1 esac When no arguments are passed, nothing executes; if I pass "" then the proper case is triggered. If I use $1 directly instead of using the temporary variable, then it works as expected. $1 I've even tried adding a specific case for "") but to no avail. "") Possible duplicate of Check existence of input argument in a Bash shell script – PesaThe 11 hours ago ...

-bash: /.bash_profile: No such file or directory [on hold]

Image
Clash Royale CLAN TAG #URR8PPP -bash: /.bash_profile: No such file or directory [on hold] After installing Java SE Development Kit 10 When I try to check which version is activated on bash and when I do the same way as the way is teached on this MOOC and on this specific course: For starting to be sure I’m on the right dir I do: cd ~ /.bash_profile I receive this error: -bash: /.bash_profile: No such file or directory And then after what can I do if the file is empty what should i put in it? If you get the same error just check that edited post. This question appears to be off-topic. The users who voted to close gave this specific reason: 1. The links provided lead to websites which are not in English language. 2. Unfortunately questions asking for: books, tutorials, or other off-site resource are off-topic and should be closed. Stack Overflow is a Questions & Answers website for specific programming questions. – sɐunıɔןɐqɐp ...

Why is the DEBUG trap executed more times than expected?

Image
Clash Royale CLAN TAG #URR8PPP Why is the DEBUG trap executed more times than expected? The following code triggers the debug trap 3 times. I am however expecting only 2 executions - one for foo , the second for echo hello : foo echo hello foo() { echo hello } set -T trap 'echo oops' DEBUG foo Output: oops oops oops hello Expected output: oops oops hello Bash versions tested: Bash GNU bash, version 4.3.30(1)-release (x86_64-unknown-linux-gnu) GNU bash, version 4.4.19(1)-release (x86_64-pc-linux-gnu) GNU bash, version 5.0.0(1)-alpha (x86_64-pc-linux-gnu) I am pretty sure I just misunderstood the manual and that I am missing something really simple/obvious here. 1 Answer 1 I assume you understand that DEBUG trap enables running the command before every other command and set -T enables the trap to all sub-shells and functions also. So once you set the trap you have three executions happ...

How to escape bash parameter expansion (when a “!” followed by letters is a parameter)

Image
Clash Royale CLAN TAG #URR8PPP How to escape bash parameter expansion (when a “!” followed by letters is a parameter) I have a bash script I need to call with a string parameter, which string starts with a exclamation mark "!" (this is mandatory in what I'm doing): myCommand.sh "!HELLO" Because of parameter expansion, bash reports an error: -bash: !HELLO: event not found Is there any way to escape the "!" that produces this? Another way to pass such parameter? Use single quotes: myCommand.sh '!HELLO' – anubhava 43 secs ago myCommand.sh '!HELLO' By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the websit...

sed find line and replace a spacific character in the last 7 characters

Image
Clash Royale CLAN TAG #URR8PPP sed find line and replace a spacific character in the last 7 characters I would like to replace a space character in the last seven characters of a line that starts with the following pattern: "start-string" "start-string" Example of the file content : randomdata 33243as4a4S3342as3A4S3A3s13aw38q363s4 sd7asd312vff74 41g5rfg45f5d3q start-string 4d d5f5d4fd4f5g6r fgdf dhfhdgnmasj 32121fdfdf21g 45s6g76t2 20 I need to replace the last space character of the line that starts with "start-string" and leave the other line as it is with no edit in the file. "start-string" Another question - how can I combine more than one regex command in sed? Welcome to SO. This site is not a code-writing service and is not meant for delivering complete solutions. Users are expected to show some effort and code whilst SO is here to help you solve specific programming problems along the way. Have you tried a...

Trying to read a file in bash and grep for a substring, store that substring in an array

Trying to read a file in bash and grep for a substring, store that substring in an array I have a file named test.txt that has some data as follows: *************************** 19. row *************************** created_at: 2018-07-02 19:57:58 deleted_at: 2018-07-03 14:17:05 serial_num: 1269043217201 *************************** 20. row *************************** created_at: 2018-07-02 19:57:58 deleted_at: 2018-07-03 14:17:05 serial_num: 1269043217202 I need to store all the serial numbers in an array. I've tried something basic to print: filename="test.txt" c=0 while read -r line do echo $line | grep serial_num | sed -n -e 's/serial_num: //p' done < "$filename" This prints data like which looks good: 1280521737734 1280521737735 1280521737736 But when I add sn=(echo $line | grep serial_num | sed -n -e 's/serial_num: //p') echo $sn This starts to print blank lines as well. How do I avoid...

How can I see file content in real time on Linux?

How can I see file content in real time on Linux? I have a log file which will be write some data in it continuously. How can I see what data is being written to the file on my Linux terminal without opening the file? 2 Answers 2 You can use tail 's -F option to follow in real time the changes made to your file: tail -F tail -F my_file.log Note: By default, tail prints the last 10 lines of your file. If you want to see more lines you can use the -n option: tail -n tail -F -n 50 my_file.log # See the last 50 lines in real time tail -f filename -n 100 tail -f filename -n 100 -n -> number of lines By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.