theuniverseisflat
theuniverseisflat

Reputation: 881

Sed / Awk parsing lines between certain charcters

I'm having hard time trying to parse the following:

Current assigned information to variable is:

position: 170.198.19.170/net1
position: 170.198.19.165/rxy
position: take1234/net 
position: imwell/net3
position: xyz/444
position: 170.198.82.142/net
position: whoareu/net

I want to print all charcters between "Position:" and the Slash "/" minus the spaces and print them on a new line

So the end result should be :

170.198.19.170
170.198.19.16
take1234
imwell
xyz  
170.198.82.142

whoareu

Can someone help please. Try some TR and Sed replace but just not getting it.

Thanking u in advance

Upvotes: 0

Views: 117

Answers (8)

Adrian Frühwirth
Adrian Frühwirth

Reputation: 45656

Using parameter expansion without spawning an external process:

$ foo="position: 170.198.19.170/net1
> position: 170.198.19.165/rxy
> position: take1234/net
> position: imwell/net3
> position: xyz/444
> position: 170.198.82.142/net
> position: whoareu/net"

$ echo "${foo//position: /}"
170.198.19.170/net1
170.198.19.165/rxy
take1234/net
imwell/net3
xyz/444
170.198.82.142/net
whoareu/net

Upvotes: 0

sgp667
sgp667

Reputation: 1875

try this:

echo "$variablename" | sed "s/position:/\npostion:/g" | cut -d"/" -f 1| cut -d " " -f 2 | sed '/^$/d'

This is updated answer where 'position:' occurs only once on ethe line, and I did it without using cut

echo "$variablename" | sed "s/position: //" | sed "s/\//\n/" | sed -n '2~2!p'

Let me know if this works for you.

Upvotes: 1

anubhava
anubhava

Reputation: 785761

Using awk:

awk -F '[:/ ]+' '$2=="position"{print $3}' file
170.198.19.170
170.198.19.165
take1234
imwell
xyz
170.198.82.142
whoareu

EDIT:

awk -F ':' '$1 ~ /position/{gsub(/ +|\/.*$/, ""); print $2}' file

Upvotes: 3

Digital Trauma
Digital Trauma

Reputation: 16016

Assuming your data is in a variable called input:

Using cut:

$ cut -d' ' -f2 <<< "$input" | cut -d/ -f1
170.198.19.170
170.198.19.165
take1234
imwell
xyz
170.198.82.142
whoareu
$ 

An interesting pure-bash way using mapfile (requires >= 4.0) and parameter expansions on the resulting array elements. The end result is your required data in the elements of the array a:

$ mapfile -t a <<< "$input"
$ a=("${a[@]%%/*}")
$ a=("${a[@]##*: }")
$ echo ${a[@]}
170.198.19.170 170.198.19.165 take1234 imwell xyz 170.198.82.142 whoareu
$ 

Another pure-bash way, using the IFS variable to separate on both and /:

$ while IFS=" /" read _ x _; do echo "$x"; done <<< "$input"
170.198.19.170
170.198.19.165
take1234
imwell
xyz
170.198.82.142
whoareu
$ 

Probably the shortest with awk:

$ awk -F '[ /]' '{print $2}' <<< "$input"
170.198.19.170
170.198.19.165
take1234
imwell
xyz
170.198.82.142
whoareu
$ 

Upvotes: 2

ghoti
ghoti

Reputation: 46876

Using sed:

$ sed -ne '/^position: /{;s/^position: //;s:/.*::;p;}' <<< "$input"
170.198.19.170
170.198.19.165
take1234
imwell
xyz
170.198.82.142
whoareu
$ 

Or awk:

$ awk '/^position: /{sub(/^position: /, ""); sub(/\/.*/,""); print;}' <<< "$input"
170.198.19.170
170.198.19.165
take1234
imwell
xyz
170.198.82.142
whoareu
$

Or just using bash:

$ while read one two ; do [ "$one" = "position:" ] && echo "${two%%/*}" ; done <<< "$input"
170.198.19.170
170.198.19.165
take1234
imwell
xyz
170.198.82.142
whoareu
$

There are MANY ways to skin this cat.

Upvotes: 1

jaypal singh
jaypal singh

Reputation: 77155

Try something like:

awk '{sub(/\/.*/,"",$2); print $2}' file

$ awk '{sub(/\/.*/,"",$2); print $2}' << EOF
position: 170.198.19.170/net1
position: 170.198.19.165/rxy
position: take1234/net 
position: imwell/net3
position: xyz/444
position: 170.198.82.142/net
position: whoareu/net
EOF
170.198.19.170
170.198.19.165
take1234
imwell
xyz
170.198.82.142
whoareu

Upvotes: 2

theuniverseisflat
theuniverseisflat

Reputation: 881

Thank you all >> In between you commands and other I came up with this

sed 's/position:[ ]*/\n/g' | sed -e 's/\/.*//

So thanks again !!!!

Upvotes: 0

Aman
Aman

Reputation: 8995

sed 's/position:[ ]*/\n/g' t|sed 's_/[a-zA-Z0-9]*__g'|sed '/^\s*$/d'

Will do the trick.

How This works?

  1. Convert position: to newlines.
  2. Remove everything beyond / in a line.
  3. Delete empty lines.

And you get what you want.

To appreciate why this was done, note that OP had the following as the input initially :

position: 170.198.19.170/net1 position: 170.198.19.165/rxy position: take1234/net position: imwell/net3 position: xyz/444 position: 170.198.82.142/net position: whoareu/net position:

Making it much more harder than the relatively trivial input now.

aman@apollo:~/entire-src/py/imgdata$ cat t
position: 170.198.19.170/net1 position: 170.198.19.165/rxy position: take1234/net position: imwell/net3 position: xyz/444 position: 170.198.82.142/net position: whoareu/net position:
aman@apollo:~/entire-src/py/imgdata$ sed 's/position:[ ]*/\n/g' t|sed 's_/[a-zA-Z0-9]*__g'|sed '/^\s*$/d'
170.198.19.170 
170.198.19.165 
take1234 
imwell 
xyz 
170.198.82.142 
whoareu 
aman@apollo:~/entire-src/py/imgdata$ 

For the updated input, cut -f2 -d' ' t|sed 's_/[a-zA-Z0-9[:punct:]]*__g' will work. Here t is the name of the file which stores the pattern.

Upvotes: 1

Related Questions