How to cut the last field from a shell string
For what it’s worth, a cut-based solution: NEW_LINE=”`echo “$LINE” | rev | cut -d/ -f2- | rev`/”
For what it’s worth, a cut-based solution: NEW_LINE=”`echo “$LINE” | rev | cut -d/ -f2- | rev`/”
The awk solution is what I would use, but if you want to understand your problems with bash, here is a revised version of your script. #!/bin/bash -vx ##config file with ip addresses like 10.10.10.1:80 file=config.txt while read line ; do ##this line is not correct, should strip :port and store to ip var ip=$( … Read more
-d ‘ ‘ means using a single space as delimiter. Since there’re 1 space before 2049 and 2 spaces before 12290, your command get them by -f 2 and -f 3. I recommend using ps aux | awk ‘{print $2}’ to get those pids. Or you can use tr to squeeze those spaces first ps … Read more
To get the output of df to display the data in kb you just need to use the -k flag: df -k Also, if you specify a filesystem to df, you will get the values for that specific, instead of all of them: df -k /example Regarding the body of your question: you want to … Read more
ps is printing out space separators, but cut without -d uses the tab character. The tr -s squeezes the spaces together to get more of the separation that you want, but remember that there is the initial set of spaces (squeezed to one) hence why you need to add 1 to each field. Also, there … Read more
You can do this easily with a variety of Unix tools: $ cut -d’ ‘ -f1 <<< “12/12/2013 14:32” 12/12/2013 $ awk ‘{print $1}’ <<< “12/12/2013 14:32” 12/12/2013 $ sed ‘s/ .*//’ <<< “12/12/2013 14:32” 12/12/2013 $ grep -o “^\S\+” <<< “12/12/2013 14:32” 12/12/2013 $ perl -lane ‘print $F[0]’ <<< “12/12/2013 14:32” 12/12/2013
Using cut: cut -d ‘ ‘ -f 2- input-file should do what you want.
This should do the trick pwd | tr “https://stackoverflow.com/” ‘\n’ If you don’t want an empty line in the beginning (due to the initial /) you could do pwd | cut -b2- | tr “https://stackoverflow.com/” ‘\n’ Example: #aioobe@r60:~/tmp/files$ pwd /home/aioobe/tmp/files #aioobe@r60:~/tmp/files$ pwd | cut -b2- | tr “https://stackoverflow.com/” ‘\n’ home aioobe tmp files
Since | is a valid regex expression, it needs to be escaped with \\| or put in square brackets: [|]. You can do this: awk -F’ \\|\\|\\| ‘ ‘{print $1}’ file Some other variations that work as well: awk -F’ [|][|][|] ‘ ‘{print “$1”}’ file awk -F’ [|]{3} ‘ ‘{print “$1”}’ file awk -F’ \\|{3} … Read more
rename -n ‘s/.{5}(.*)/$1/’ * The -n is for simulating; remove it to get the actual result.