Eduardo
Eduardo

Reputation: 397

AWK or sed need to remove lines from output if a certain pattern is found

I need to launch something, to verify a file. A text separated in two fields by a comma, on the second field we may find /*/*/* which is data I want. I want to discard /*/* only

Input file

\\CIFSERVER1\Share1,/fs1_casa/c/share1
\\CIFSERVER1\Share2,/fs2_casa/c/share2
\\CIFSERVER1\Share3,/fs1_casa/c/share3
\\EDULIN\edu,/edu1
\\CIFSERVER2\root,/fs1_casa
\\CIFSERVER2\root,/fs2_casa
\\CIFSERVER2\root,/fs3_casa
\\CIFSERVER2\root,/fs1_casa

Output should be:

\\CIFSERVER1\Share1,/fs1_casa/c/share1
\\CIFSERVER1\Share2,/fs2_casa/c/share2
\\CIFSERVER1\Share3,/fs1_casa/c/share3

What should be removed?

\\EDULIN\edu,/edu1
\\CIFSERVER2\root,/fs1_casa
\\CIFSERVER2\root,/fs2_casa
\\CIFSERVER2\root,/fs3_casa
\\CIFSERVER2\root,/fs1_casa

Upvotes: 3

Views: 958

Answers (4)

William Pursell
William Pursell

Reputation: 212634

If you are trying to print all lines that contain more than 3 / after a single , you could just do:

sed -n '\@,.*/.*/.*/@p' input

To restrict to those lines containing exactly 3 /:

sed -n '\@,\([^/]*/\)\{3\}[^/]*$@p' input

If you must restrict the search to the second field (eg, there may be more than 1 comma):

awk '$2 ~ /[/].*[/].*[/]/' FS=, input

or

awk -F, '$2 ~ /\/.*\/.*\//'  input

Upvotes: 1

With only two fields, this is easily done with grep: retain only the lines that have three / characters after the comma.

grep ',/.*/.*/'

With more fields, while grep or sed would work, it's easier with awk.

awk -F , '$2 ~ /(\/.*){3}/ {print}'

Upvotes: 1

tripleee
tripleee

Reputation: 189908

If you want three or more slashes after a comma,

grep ',/.*/.*/' file

Upvotes: 2

Jonathan Leffler
Jonathan Leffler

Reputation: 755016

If you want to keep those entries which have 3 or more slashes after the comma, removing those lines with two or less, then I'd use:

sed -n '/[^,]*,\/[^/]*\/[^/]*\//p'

The -n suppresses normal printing. The pattern looks for slash followed by non-slashes, another slash, more non-slashes, and another slash and prints it. If you must have a character after the third slash, add [^/] after the third \/ in the regex.

Upvotes: 1

Related Questions