How to get to previous line of a file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get to previous line of a file?
# 1  
Old 10-07-2010
Question How to get to previous line of a file?

The situation is following :
  1. I have a file from which I am reading record one by one.
  2. While reading I have to check if any line starts with the value "SE".
  3. If, it finds any value starts with "SE", then I have to check whether its preceding line starts with "NE" or not.
  4. If the preceding line starts with "NE", then I have to extract some data from the "NE" line using cut.
  5. Else, I have to to extract some data from the "SE" line using cut.
Please help me to find a way to resolve this problem.

Thanks in advance
# 2  
Old 10-07-2010
Bug

Try this
Code:
#!/bin/ksh

line=Start
line1=Start

while read line
do
var1=`echo ${line} | cut -c 1-2`
var2=`echo ${line1} | cut -c 1-2`
if [ $var1 = 'SE' ]
then
    if [ $var2 = 'NE' ]
    then
    echo $line1 >>Output.txt
    else
    echo $line >>Output.txt
    fi
fi
line1=$line
done<DemoFile.txt

You can change the code as per your requirement.
# 3  
Old 10-07-2010
bash code:
  1. #!/bin/bash
  2. while read L
  3. do
  4.    S1=${L::2}
  5.    if &#91; $S1 = SE ]
  6.    then
  7.       &#91; $S2 = NE ] && OUT="$L2" || OUT="$L"
  8.       echo "$OUT" | cut whatever_you_want_but_i_don_t_know
  9.    fi
  10.    L2="$L"
  11.    S2=${L::2}
  12.    fi
  13. done   <infile
# 4  
Old 10-07-2010
Quote:
Originally Posted by mady135
The situation is following :
  1. If the preceding line starts with "NE", then I have to extract some data from the "NE" line using cut.
Why using cut ? is this a homework? Please post a data sample and expected output.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding line in a file using info from previous line

I have a shell script that looks something like the following: mysql -uroot db1 < db1.sql mysql -uroot db2 < db2.sql mysql -uroot db3 < db3.sql mysql -uroot db4 < db4.sql .... different db names in more than 160 lines. I want to run this script with nohup and have a status later. So,... (6 Replies)
Discussion started by: MKH
6 Replies

2. UNIX for Advanced & Expert Users

How to find a string in a line in UNIX file and delete that line and previous 3 lines ?

Hi , i have a file with data as below.This is same file. But actual file contains to many rows. i want to search for a string "Field 039 00" and delete that line and previous 3 lines in that file.. Can some body suggested me how can i do using either sed or awk command ? Field 004... (7 Replies)
Discussion started by: vadlamudy
7 Replies

3. UNIX for Dummies Questions & Answers

How to remove fields space and append next line to previous line.?

awk 'BEGIN{FS = "Ç"} NR == 1 {p = $0; next} NF > 1 {print p; p = $0} NF <= 1 {p = (p " " $0)} END {print p}' input.txt > output.txt This is what the input data file looks like with broken lines Code: 29863 Ç890000000 Ç543209911 ÇCHNGOHG Ç000000001 Ç055 ... (4 Replies)
Discussion started by: cumeh1624
4 Replies

4. 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

5. Shell Programming and Scripting

Sed Comparing Parenthesized Values In Previous Line To Current Line

I am trying to delete lines in archived Apache httpd logs Each line has the pattern: <ip-address> - - <date-time> <document-request-URL> <http-response> <size-of-req'd-doc> <referring-document-URL> This pattern is shown in the example of 6 lines from the log in the code box below. These 6... (1 Reply)
Discussion started by: Proteomist
1 Replies

6. Shell Programming and Scripting

Delete line with match and previous line quoting/escaping problem

Hi folks, I've list of LDAP records in this format: cat cmmac.export.tmp2 dn: deviceId=0a92746a54tbmd34b05758900131136a506,ou=devices,ou=customer,ou=nl,o=upc cmmac: 00:13:11:36:a5:06 dn: deviceId=0a92746a62pbms4662299650015961cfa23,ou=devices,ou=customer,ou=nl,o=upc cmmac:... (4 Replies)
Discussion started by: tomas.polak
4 Replies

7. 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

8. Shell Programming and Scripting

Append each line to next previous line in a file

Hi all, Please help me in providing sample code to append the following 4 lines in one row. Input : A1/EXT "BAPBSC10/07B/00" 523 090530 0115 RXOCF-430 HY1711 1 EXTERNAL ALARM DOOR ALARM Output should be : A1/EXT "BAPBSC10/07B/00" 523 090530 0115 ... (8 Replies)
Discussion started by: sudhakaryadav
8 Replies

9. Shell Programming and Scripting

Perl how to move pointer to previous line in a txt file?

I have a text file that has blocks of text. Each block starts with ### and ends with End_###. I wrote a perl script to search a string from line 2 (ignore any line starts with ###) of each block if matched, need to print that whole block. According to the input file in below, it will print... (5 Replies)
Discussion started by: tqlam
5 Replies

10. Shell Programming and Scripting

search and retrieve previous line in file

I've got a file with the following layout: #STMP FSgroup filename /filesysname1 filestatus 2 #STMP FSstatus filename /filesysname1 ratio 30 #STMP FSgroup filename ... (2 Replies)
Discussion started by: paulsew
2 Replies
Login or Register to Ask a Question