how to cut the last field using cut linux bash? [duplicate] - linux

This question already has answers here:
How to find the last field using 'cut'
(14 answers)
Closed 4 years ago.
Hello everyone i want to know to cut the last field with separator :
without knowing how many fields that i have any ideas please .
is there any option for command cut .

You can revert the string and then print 1st character. Itself cut can't work from backwards.
echo "Your string ABC" | rev | cut -c 1

Awk is the right tool for this. Try :
ls -lh | awk '{ print $NF }'

Related

How to delete perticular lines and its nearby lines in Linux shell? [duplicate]

This question already has answers here:
How do I delete a matching line, the line above and the one below it, using sed?
(7 answers)
Closed 1 year ago.
I have a file named file.txt and it contains several lines containing string "NaN". How can I delete lines containing "Nan" and one line before and after it. I know sed -i '/pattern/d' file.txt can delete the matched line, but how can I delete neaby lines of the matched line.
Best regards
This is a most inelegant solution, but works. Perhaps, it will anger a UNIX guru to come and provide a real answer.
grep 'Nan' -n -C 1 target_file.txt | awk -F '[-:]' '{print $2}' | sed '2d' | paste -d, - - | sed 's/$/d/' > del_lines.sed && sed -f del_lines.sed target_file.txt > output_file.txt
fyi, The word "perticular" is misspelled, its "par-" like in golf!

How to take a text between "/" with Awk / cut? [duplicate]

This question already has answers here:
shell script to extract text from a variable separated by forward slashes
(3 answers)
Closed 4 years ago.
I have this command in a script:
find /home/* -type d -name dev-env 2>&1 | grep -v 'Permiso' >&2 > findPath.txt
this gives me this back:
/home/user/project/dev-env
I need to take the second parameter between "/" (user) to save it later in a variable. I can not find the way to just pick up the "user" text.
Using cut:
echo "/home/user/project/dev-env" | cut -d'/' -f3
Result:
user
This tells cut to use / as the delimiter and return the 3rd field. (The 1st field is blank/empty, the 2nd field is home.)
Using awk:
echo "/home/user/project/dev-env" | awk -F/ '{print $3}'
This tells awk to use / as the field-seperator and print the 3rd field.
Assuming that the path resulting from the grep is always an absolute path:
second_component=$(find .... -type d -name dev-env 2>&1 | grep -v 'Permiso' | cut -d / -f 3)
However, your approach suffers from several other problems:
You use /home/* as starting point for find. This will work only, if there is exactly one subdirectory below /home. Not a very likely scenario.
Even then, it works only if grep results in exactly one line. This is a semantic problem: What if you get more than one line - which one are you interested in? Assume that you know that you are always interested into the first line, you can solve this by piping the result though head -n 1.
Next, you redirect the stderr from find to stdout, which means that any error from find remains unnoticed; you just get some weird result. It would be better to have any error message from find being displayed, and instead evaluate the exit code from find and grep.
... | cut -d/ -f3
"Third field, as cut by slash delimiter"

how to print text between two specific words using awk, sed? [duplicate]

This question already has answers here:
How to use sed/grep to extract text between two words?
(14 answers)
Closed 4 years ago.
how to print text between two specific words using awk, sed ?
$ ofed_info | awk '/MLNX_OFED_LINUX/{print}'
MLNX_OFED_LINUX-4.1-1.0.2.0 (OFED-4.1-1.0.2):
$
Output required:-
4.1-1.0.2.0
Following awk may help you here.(considering that your input to awk will be same as shown sample only)
your_command | awk '{sub(/[^-]*/,"");sub(/ .*/,"");sub(/-/,"");print}'
Solution 2nd: With sed solution now.
your_command | sed 's/\([^-]*\)-\([^ ]*\).*/\2/'
Solution 3rd: Using awk's match utility:
your_command | awk 'match($0,/[0-9]+\.[0-9]+\-[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/){print substr($0,RSTART,RLENGTH)}'
You may use this sed:
echo 'MLNX_OFED_LINUX-4.1-1.0.2.0 (OFED-4.1-1.0.2):' |
sed -E 's/^[^-]*-| .*//g'
4.1-1.0.2.0
This sed command removes text till first hyphen from start or text starting with space towards end.
Try this:
ofed_info | sed -n 's/^MLNX_OFED_LINUX-\([^ ]\+\).*/\1/p'
The sed command only selects lines starting with the keyword and prints the version attached to it.

display words between semi colons with sed or awk [duplicate]

This question already has answers here:
Unable to separate semi-colon separated line awk
(3 answers)
Closed 5 years ago.
I would like to display fields first and third in file full on entries like the one below
first;second;third;four
Simply with cut command:
cut -d';' -f1,3 file
Simple awk could help you here.
awk -F";" '{print $1,$3}' Input_file

Extract text between two given different delimiters in a given text in bash [duplicate]

This question already has answers here:
Print text between delimiters using sed
(2 answers)
Closed 2 years ago.
I have a line of text which looks like hh^ay-pau+h#ow, I want to extract the text between - and + which in this case is pau. This should be done in bash. Any help would be appreciated.
EDIT: I want to extract the text between the first occurence of the tokens
PS: My google search didn't take me anywhere. I apologize if this question is already asked.
The way to do this in pure bash, is by using parameter expansions in bash
$ a=hh^ay-pau+h#ow
$ b=${a%%+*}
$ c=${b#*-}
$ echo $c
pau
b: remove everything including and behind the first + occurence
c: remove everything excluding and before the first - ocurrence
More info about substring removing in bash parameter expansion
Try
grep -Po "(?<=\-).*?(?=\+)"
For example,
echo "hh^ay-pau+h#ow" | grep -Po "(?<=\-).*?(?=\+)"
If you have only one occurence of - and + you can use cut:
$ echo "hh^ay-pau+h#ow" | cut -d "-" -f 2 | cut -d "+" -f 1
pau
Assuming one occurence of + and -, you can stick to bash
IFS=+- read -r _ x _ <<<'hh^ay-pau+h#ow'
echo $x
pau
If you're guarenteed to only have one - and one + .
% echo "hh^ay-pau+h#ow" | sed -e 's/.*-//' -e 's/+.*//'
pau
echo "hh^ay-pau+h#ow" | awk -F'-' '{print $2}' |awk -F'+' '{print $1}'

Resources