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.

For example my file is like:

59 6 18 2014 169 7 14 2 7671912 7849744 201 4
60 6 19 2014 170 5 49 2 7671912 7849744 201 5
61 6 20 2014 171 6 8 2 7671912 7849744 201 6
62 6 23 2014 174 5 3 2 7671912 7849744 201 7
63 6 23 2014 174 7 17 2 7671912 7849744 201 8
64 6 24 2014 175 16 13 2 7671912 7849744 201 9
65 6 25 2014 176 8 20 2 7671912 7849744 201 10
10
1 1 49 6 16 2014 7 39 201 1 0 1043 <au_launchpad_back.gif> 201 gif <SIZE> 871
1 2 49 6 17 2014 13 15 201 1 0 1043 <au_launchpad_back.gif> 201 gif <SIZE> 871
1 3 49 6 18 2014 1 38 201 1 0 1043 <au_launchpad_back.gif> 201 gif <SIZE> 871
1 4 49 6 18 2014 7 14 201 1 0 1043 <au_launchpad_back.gif> 201 gif <SIZE> 871
1 5 49 6 19 2014 5 49 201 1 0 1043 <au_launchpad_back.gif> 201 gif <SIZE> 871

I need to print the previous line of single token entry "10" which should be

65 6 25 2014 176 8 20 2 7671912 7849744 201 10

How can I do this?

share|improve this question

3 Answers 3

up vote 7 down vote accepted

You can use awk:

awk 'NF == 1 { print LAST } { LAST=$0 }' < datafile

This saves every line of the file (LAST=$0) as it goes, and when a line has only one record (NF == 1 - NF is the number of tokens on the line, roughly speaking) it prints the saved previous line out.

share|improve this answer
    
+1 but it might be better to use $0 == "10" instead. I think the OP meant a specific "token". –  terdon Jun 27 at 10:54
    
thanks a ton its working :) –  nalini Jun 27 at 11:00
    
now i need to traverse through the result and need to check 9th position,11th position,13th,15th etc till the end of the line with the consecutive odd positions and if those are not matching with any of the mentioned numerics 201 230 197 193 229 200 215 226 001 198 it should throw an print the error as CORRUPTED, if not AVAILABLE should be printed. (if we cosider this as example 9th position is 7671912) NOTE : The code should be feasible at any other file as well. –  nalini Jun 27 at 15:07
    
now i need to traverse through the result and need to check 9th position,11th,13th etc till the end of the line with the consecutive odd positions and if those positions are not matching with any of the mentioned numerics 201 230 197 193 229 200 215 226 001 198 it should print the error as CORRUPTED,if not AVAILABLE should be printed. (if we cosider this as example 9th position is 7671912) NOTE : The code should be feasible at any other file as well. The code which i wrote for the result is #! /bin/ksh file=input.txt while read line do awk 'NF == 1 { print LAST } { LAST=$0 }' done < "$file" –  nalini Jun 27 at 15:17

If you have only one occurrence of the token, you could do this with grep and head:

grep -B 1 '^10$' file | head -n 1

The -B 1 tells grep to print the matching line and the one before it. The ^ means "match the beginning of the line" and $ means "match the end". Therefore, ^10$ will match all lines that contain 10 alone. Finally, the head -n 1 prints only the first of the resulting lines.

share|improve this answer

sed

You can use sed:

sed '/^10$/{x;p};x;d' < datafile

Explanation:

The address /^10$/ matches the line containing 10. When it matches, the following series of commands is run, {x;p}, which exchanges (x) the hold space with the pattern space and then prints (p) the pattern space.

The hold space contains the previous line because of the rest of the script. For each line of the file, exchange (x) the hold space with the pattern space, and then delete (d) whatever is in the pattern space.

Unlike the first command, which has an address prefix that causes it to only match when the 10 line is seen, these commands have no prefix, and thus match on every line of the file. The net result is that we keep putting the last line seen into the hold space, and the fact that we delete the pattern space means that there is no output other than our explicit output when we see 10.

share|improve this answer
    
As Michael homer suggested, the command is working, now I need to check the 9th position,11th,13th etc till the end of the line with the consecutive odd positions and if those positions are not matching with any of the mentioned numerics 201 230 197 193 229 200 215 226 001 198 it should print the error as CORRUPTED,if not AVAILABLE should be printed. Note : The code should be feasible for any kind of the data –  nalini Jul 2 at 6:15
    
You can use cut or awk to break up the line and extract the items you wish to test (or perl, or other things), and you can use grep -F -f <file> to test them against a fixed list of strings, but you should really ask another question about this. –  David Conrad Jul 2 at 17:56

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.