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

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

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 cut the last field using cut linux bash? [duplicate]

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 }'

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.

How to remove "-" and a space from the beginning in a bash script? [duplicate]

This question already has answers here:
Editing/Replacing content in multiple files in Unix AIX without opening it
(2 answers)
Closed 6 years ago.
I have an output that looks as below
- 0.1-1
- 0.1-2
- 0.1-3
- 0.1-6
- 0.1-7
- 0.1-9
How to use grep or something else so as to remove "-" and a space from the beginning.
0.1-1
0.1-2
0.1-3
0.1-6
0.1-7
0.1-9
With sed:
sed -e 's/^- //' input.txt
Or with GNU grep:
grep -oP '^- \K.*' input.txt
You may use grep also,
grep -oE '[0-9].*' file
With awk:
awk '{print $2}' file
You can use cut to remove the first two columns of every line:
cut -c3- input.txt

How to grep for specific pattern in a file [duplicate]

This question already has answers here:
Can grep show only words that match search pattern?
(15 answers)
Closed 8 years ago.
I have a bash file that has below line along with other lines.
var BUILD_VERSION = '2014.17.10_23';
I just want to extract 2014.17.10_23 and this value may change so something like grep for 2014* . However when I do that I get the whole line returned instead of the value 2014.17.10_23.
What would be the best way to achieve this?
Thanks
Using awk:
awk -F= '/BUILD_VERSION/{print $2}' input | tr -d "[' ;]"
And with sed:
sed -n "/BUILD_VERSION/s/.*'\([^']*\)'.*/\1/p" input
grep 'BUILD_VERSION' <your file> | sed -e 's/var BUILD_VERSION = //g'
Would get you '2014.17.10_23'; tweak the sed expression (or pipe it through a few more) to get rid of quotes.
It would be a 1 liner regex in Perl...
Here is another awk solution:
awk -F' = ' '/BUILD_VERSION/ {gsub(/\x27|;/,""); print $NF}'
You can use this awk
awk -F\' '/BUILD_VERSION/ {print $2}' file
2014.17.10_23

Resources