Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a file of the following form (with '-' serving as delimiters), and I want to find the appearance of a number only when it follows a delimiter. I suppose it's a concatenation of grep '-\n' and $number, but I can't find the way to do it right. thanks..

1400
2
132
342
-
76567
1
1234
-
87
2
1400
54
-
share|improve this question
1  
By modifying your question like that 3 months later, you're invalidating all the answers you've received. It's also unlikely that people will notice that you've changed the requirements. It would be better to ask a separate question. –  Stéphane Chazelas Jun 17 at 22:01

5 Answers 5

To find variable before the delimiter:

Using awk:

$ awk '/!!!!!!!!!!!/{print num}{num=$0}' inputFile
342
1234
54

or gnu-awk:

$ gawk 'NF && $0=$NF' RS='[!]+'  inputFile
342
1234
54

To find variable after the delimiter:

Using awk:

$ awk '/!!!!!!!!!!!/{if(getline) print $0}' inputFile
76567
87

or gnu-awk:

$ gawk 'NR>1 && $0=$1' RS='[!]+'  inputFile
76567
87
share|improve this answer
    
we can shorten your version: awk '/!!!!!!!!!!!/{if(getline){print}}' file –  cuonglm Mar 16 at 13:20
1  
Good that you have provided both the variants; given that the question doesn't make it clear! –  devnull Mar 16 at 13:23
    
Thanks @devnull. Yea, I was pretty much certain what I had in earlier was what OP wanted until a new answer came in which completely threw my understanding of the question. So went with both! :) –  jaypal singh Mar 16 at 13:25
    
@jaypalsingh The level of tolerance for not-so-good-quality-posts is considerably higher on this site in comparison to SO. As such, rather vague questions keep getting attracted. –  devnull Mar 16 at 13:31
1  
@jaypalsingh nice!!!!!! :D –  Rahul Patil Mar 16 at 13:33

The following command

grep '!!' -A 1 file|grep -vE '!!|\-\-'

will yield

76567
87
share|improve this answer
    
You read my mind... –  orion Mar 16 at 13:12

grep has a switch -A that tells it to print a number of lines after the match. In this case, just use -A 1 and you'll get ouptut like

!!!!!!!!!!!
76567
--
!!!!!!!!!!!
87
--
!!!!!!!!!!!

Now just grep out the numbers with | grep -e '[0-9]'.

share|improve this answer
    
grep --no-group-separator -A1 '!' inputFile | grep -v '!' would be better. –  jaypal singh Mar 16 at 14:19

using AWK

To find variable before the delimiter:

awk '{ print $NF }' RS='!!!!!!!!!!!'  infile

Output

342
1234
54

To find variable after the delimiter:

awk 'NR>1{ print $1 }' RS='!!!!!!!!!!!'  infile

Output

76567
87
share|improve this answer

This does a number before a delimiter:

sed -n '/[^0-9]/!h;/^-$/{g;/./p}'

And this after:

sed -n '/^-$/{n;/./!d;/[^0-9]/!p}'
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.