Reputation: 613
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
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
Reputation: 203169
$ sed -n 's/^AAA *//p' file
http://hello.world.com/12345/11.*aa*
Upvotes: 2
Reputation: 3079
grep -oP "^AAA.*\Khttp.*" file
Output:
http://hello.world.com/12345/11.*aa*
Upvotes: 0
Reputation: 92854
Simply with awk (assuming that url is the last item of each line):
awk '/^AAA/{ print $NF }' file.txt
Upvotes: 1
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