Issue
I have file containing text like this
4539 DECK AAA
OO-99999999-99999999-99999999-99999999 -99999999-99999999 259800
259800 259800-99999999-99999999 4539 DECK ABC
OO-99999999-99999999-99999999-99999999 -99999999-99999999 259800
259800 259800-99999999-99999999 4539 DECK ABA
OO-99999999-99999999-99999999-99999999 -99999999-99999999 259800
259800 259800-99999999-99999999 4539 DECK ABD
OO-99999999-99999999-99999999-99999999 -99999999-99999999 259800
259800 259800-99999999-99999999
I want to extract a selected port from it. So the output must look like this. Without preceding and trailing spaces tabs
AAA
ABC
ABA
ABD
I used this but it select entire line after text. Is there any way using only sed. Not interested in any other solution
sed "s/.*DECK[[:blank:]](.[A-Z])*/\1/"
Solution
You can use
sed -n '/.*DECK[[:blank:]]*\([[:upper:]]*\).*/s//\1/p'
Details:
n
- suppresses default line output/.*DECK[[:blank:]]*\([[:upper:]]*\).*/
- zero or more chars,DECK
and then zero or more horizontal whitespaces are matched, then zero or more uppercase letters are consumed and put into Group 1, and then the rest of the string (here, line) is matcheds//\1/p
- the match from the pattern above (empty pattern tells sed to take the one that was used previously) is replaced with the Group 1 value, and only this value isp
rinted.
See the online demo:
#!/bin/bash
s='4539 DECK AAA
OO-99999999-99999999-99999999-99999999 -99999999-99999999 259800
259800 259800-99999999-99999999 4539 DECK ABC
OO-99999999-99999999-99999999-99999999 -99999999-99999999 259800
259800 259800-99999999-99999999 4539 DECK ABA
OO-99999999-99999999-99999999-99999999 -99999999-99999999 259800
259800 259800-99999999-99999999 4539 DECK ABD
OO-99999999-99999999-99999999-99999999 -99999999-99999999 259800
259800 259800-99999999-99999999'
sed -n '/.*DECK[[:blank:]]*\([A-Z]*\).*/s//\1/p' <<< "$s"
Output:
AAA
ABC
ABA
ABD
Answered By - Wiktor Stribiżew Answer Checked By - Katrina (WPSolving Volunteer)