I want to remove a specific word from string in shell script.
My text file contains below data
***** Failed tests *****
Devices Class Test
Nexus_5_29_2(AVD) - 10 Addition productCalculation
I want to remove Devices
and Nexus_5_29_2(AVD) - 10
Nexus_5_29_2(AVD) - 10
is not constant it may change to Nexus_5_29_1(AVD) - 10
or Nexus_5_29(AVD) - 10
Expected output is
***** Failed tests *****
Class Test
Addition productCalculation
how can i achive this? awk
is preferable
Use sed for replace or search and delete
sed -i -e 's/Devices//g' filename.txt
Awk solution:
cat filename.txt | awk '{print $2 $3 }'
To do as you want, I need more information.
This might work for you (GNU sed):
sed -E '/^Devices/{N;s/^\S+\s+(.*\n)Nexus_5_29(_[0-9]+)?\(AVD\) - 10\s+/\1/};P;D' file
If a line begins Devices
append the next line and remove the first column and its associated whitespace of the those two lines if the second line matches the required string.
NB The appended line may not match the criteria for the second line, in which case the first line should be printed as normal and the process repeated with the second line taking its place, hence the reason for P
and D
commands. All other lines will be printed as normal.
From the criteria you described, the above solution may well be suffice. If, however a more generic solution is needed, perhaps?:
sed -E '/^Devices/{N;s/^\S+\s+(.*\n)\w+_[0-9]+_[0-9]+(_[0-9]+)?\([A-Z]{3}\) - [0-9]+\s+/\1/};P;D' file
Assuming that your file has only spaces and no tabs, you can delete the first n characters for the rows except the header.
> awk 'NR>1 {$0=substr($0,26)} 1' file
***** Failed tests *****
Class Test
Addition productCalculation
find the position of the second header in second line and trim left side.
$ awk 'NR==2{n=match($0,/ [^ ]/)} {print substr($0,n+1)}' file
***** Failed tests *****
Class Test
Addition productCalculation
Assumes headers are one word each (without spaces).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.