Andy
Andy

Reputation: 613

Printing after specific string

I have a text file, containing the following text lines

AAA http://hello.world.com/12345/11.*aa*
AAB AAC http://hello.world.com/12346/11.*aa*

How can I print url for line starting with AAA?

cat file.txt | tr -d ' ' | grep -i 'AAA' | cut -f2 -d':'

Above would only display //hello.world.com/12345/11.*aa* but I could of course harcode http: when I expand variable it's just a messy way of doing things.

Upvotes: 0

Views: 520

Answers (5)

Benoît Zu
Benoît Zu

Reputation: 1287

cat file.txt | grep "^AAA" | rev | cut -d ' ' -f1 | rev

Output: http://hello.world.com/12345/11.*aa*

Details:

cat file.txt: Print the content of the file.

grep "^AAA": Keep only line begining by "AAA".

rev: Reverse the characters of each lines.

cut -d ' ' -f1: Split each line using space as delimiter and keep the first column.

rev: Reverse the characters of each lines.

Upvotes: 0

Ed Morton
Ed Morton

Reputation: 203169

$ sed -n 's/^AAA *//p' file
http://hello.world.com/12345/11.*aa*

Upvotes: 2

Rahul Verma
Rahul Verma

Reputation: 3079

grep -oP "^AAA.*\Khttp.*" file

Output:

http://hello.world.com/12345/11.*aa*

Upvotes: 0

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

Simply with awk (assuming that url is the last item of each line):

awk '/^AAA/{ print $NF }' file.txt

Upvotes: 1

EdmCoff
EdmCoff

Reputation: 3576

Assuming the criteria is that the URL is always after the last space in the line (at least for lines starting with AAA):

$: echo "AAA http://hello.world.com/12345/11.*aa*" | grep -i "^AAA" | sed 's/^.* //g'
http://hello.world.com/12345/11.*aa*

The sed bit matches all text from the beginning of the line up to the last space and removes it.

The echo is just an example I tested with. You would use:

grep -i "^AAA" file.txt | sed 's/^.* //g'

Upvotes: 0

Related Questions