Sponsored Content
Top Forums Shell Programming and Scripting Any solution to get previous line in awk Post 302844352 by Jotne on Monday 19th of August 2013 12:45:48 AM
Old 08-19-2013
Code:
awk '
	/pattern/ {print a}
	{a=$0}'

Running on a file:
Line1: Does it contain "pattern", yes print a (here a will be empty), no continue
a=Line1
Line2: Does it contain "pattern", yes print a (her a contial line1), no continue
a=Line2
Line3: Does it contain "pattern", yes print a (her a contial line2), no continue
a=Line3

So a does always contain previous line.
This User Gave Thanks to Jotne For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk;sed appending line to previous line....

I know this has been asked before but I just can't parse the syntax as explained. I have a set of files that has user information spread out over two lines that I wish to merge into one: User1NameLast User1NameFirst User1Address E-Mail:User1email User2NameLast User2NameFirst User2Address... (11 Replies)
Discussion started by: walkerwheeler
11 Replies

2. Shell Programming and Scripting

Awk script to create new file based on previous line

Need help creating a script that does the following: Sort a file Compare the previous line "last field" with current line "last field" If they are the same, print output to a file If they are different print output to a new file The script should keep creating new files if the previous... (5 Replies)
Discussion started by: Muga801
5 Replies

3. Shell Programming and Scripting

awk get matched line's previous line

hi everyone, a b in c d e f in g output is: a e so awk search for "in", then print out the matched line's previuos line. Please advice. (11 Replies)
Discussion started by: jimmy_y
11 Replies

4. Shell Programming and Scripting

Awk help with source and previous line loop

Hello, I've written a ksh awk script to ping multiple servers and write the results to a file. That part is working ok. I then want to extract the names of only the server which are available. This is indicated by '1 packets received'. The server name actually appears above that line so I found... (4 Replies)
Discussion started by: Grueben
4 Replies

5. UNIX for Dummies Questions & Answers

Awk to print data from current and previous line

Hi guys, I have found your forum super useful. However, right now I am stuck on a seemingly "simple" thing in AWK. I have two columns of data, the first column in Age (in million years) and the second column is Convergence Rate (in mm/yr). I am trying to process my data so I can use it to... (2 Replies)
Discussion started by: awk_noob_456
2 Replies

6. Shell Programming and Scripting

Read column values from previous and next line using awk

Hi, I have a csv file which contains data that looks something like this: Key1 Key2 Key3 New_Key1 New_Key2 New_Key3 102 30 0 - - - 102 40 1 30 40 50 102 50 2 40 50 ... (4 Replies)
Discussion started by: Nishi_Licious
4 Replies

7. Shell Programming and Scripting

Remove previous line if next & previous lines have same 4th character.

I want to remove commands having no output. In below text file. bash-3.2$ cat abc_do_it.txt grpg10so>show trunk group all status grpg11so>show trunk group all status grpg12so>show trunk group all status GCPKNYAIGT73IMO 1440 1345 0 0 94 0 0 INSERVICE 93% 0%... (4 Replies)
Discussion started by: Raza Ali
4 Replies

8. Shell Programming and Scripting

awk script -print line when $2 > $2 of previous line

Hi all, From a while loop I am reading a sorted file where I want to print only the lines that have $1 match and $2 only when the difference from $2 from the previous line is > 30. Input would be like ... AN237 010 193019 0502 1 CSU Amoxycillin AN237 080 ... (2 Replies)
Discussion started by: gafoleyo73
2 Replies

9. Shell Programming and Scripting

awk to insert line previous to a pattern?

I have a very long line with certain patters embedded in there. I need to be able to read that line, and when it encounters that pattern, create a new line. I want the pattern to be the beginning of the new line. I thought sed or awk could do this, but everything I try in sed gives me a "sed... (2 Replies)
Discussion started by: Drenhead
2 Replies

10. Shell Programming and Scripting

awk to copy previous line matching a particular columns

Hello Help, 2356798 7689867 999 000 123678 20385907 9797 666 17978975 87468976 968978 98798 I am trying to have out put which actually look for the third column value of 9797 and then it insert line there after with first, second column value exactly as the previous line and replace the third... (3 Replies)
Discussion started by: Indra2011
3 Replies
GREP(1) 						      General Commands Manual							   GREP(1)

NAME
grep - search a file for a pattern SYNOPSIS
grep [ option ... ] pattern [ file ... ] DESCRIPTION
Grep searches the input files (standard input default) for lines (with newlines excluded) that match the pattern, a regular expression as defined in regexp(6). Normally, each line matching the pattern is `selected', and each selected line is copied to the standard output. The options are -c Print only a count of matching lines. -h Do not print file name tags (headers) with output lines. -i Ignore alphabetic case distinctions. The implementation folds into lower case all letters in the pattern and input before interpre- tation. Matched lines are printed in their original form. -l (ell) Print the names of files with selected lines; don't print the lines. -L Print the names of files with no selected lines; the converse of -l. -n Mark each printed line with its line number counted in its file. -s Produce no output, but return status. -v Reverse: print lines that do not match the pattern. Output lines are tagged by file name when there is more than one input file. (To force this tagging, include /dev/null as a file name argument.) Care should be taken when using the shell metacharacters $*[^|()= and newline in pattern; it is safest to enclose the entire expression in single quotes '...'. SOURCE
/sys/src/cmd/grep.c SEE ALSO
ed(1), awk(1), sed(1), sam(1), regexp(6) DIAGNOSTICS
Exit status is null if any lines are selected, or non-null when no lines are selected or an error occurs. GREP(1)
All times are GMT -4. The time now is 05:34 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy