replace [previous] line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replace [previous] line
# 1  
Old 07-27-2007
replace [previous] line

Hi ,suppose I have a file
title=dsafsadf
........
.......
year=1995
author=john smith
............
title=bbbbbb
........
.......
year=1988
author=alex

I need to replace the title line with a expression that contains variables year and author. I want to use a python readline for loop. The problem is that I couldn't use commands such as 'print data[i-3].strip()' because I don't know exactly how many lines are between the current line and title line.
Is there any way to do this?
# 2  
Old 07-27-2007
Code:
#!/bin/ksh
mInpFile='Your_File'
mYearAuthor='/tmp/YearAuthor'
egrep 'year|author' ${mInpFile} > ${mYearAuthor}
while read mLine
do
  mFirstSix=`echo $mLine | cut -c1-6`
  if [ "${mFirstSix}" = "title=" ]; then
    mYear=`sed -n '1p' ${mYearAuthor} | cut -d'=' -f2`
    mAuthor=`sed -n '2p' ${mYearAuthor} | cut -d'=' -f2`
    mOutLine='title='${mYear}${mAuthor}
    sed '1,2d' ${mYearAuthor} > $$Temp
    mv $$Temp ${mYearAuthor}
  else
    mOutLine=${mLine}
  fi
  echo ${mOutLine}
done < ${mInpFile}
rm -f ${mYearAuthor}

# 3  
Old 07-27-2007
fantastic!
I see how you managed to match the year/author of book1 to the title of book1 and year/author of book2 to title of book2. You would delete the first two lines of the YearAuthor after each iteration.How genious! Algorithm is not something you can learn from comptuer science class.

Last edited by grossgermany; 07-27-2007 at 05:44 PM..
# 4  
Old 07-27-2007
Quote:
mYear=`sed -n '1p' ${mYearAuthor} | cut -d'=' -f2`
How about combining this, sed and cut to just awk

Code:
mYear=`awk -F"=" 'NR == 1 { print $2 }' filename`

or

Code:
mYear=`awk -F"=" ' { if ( NR == 1 ) { print $2 } else { exit } }' filename`

# 5  
Old 07-27-2007
One minor defect with the ksh script is that ksh seem to delete leading white space in the 'while ' loop. There $mline is not exactly the same as each line of input file.
so output is missing leading white space.
# 6  
Old 07-27-2007
First change the value of the IFS variable before the read statement and place double qoutes around the variable like this:

Code:
#!/bin/ksh
mInpFile='Your_File'
mYearAuthor='/tmp/YearAuthor'
egrep 'year|author' ${mInpFile} > ${mYearAuthor}
OLDIFS=$IFS
IFS=
while read mLine
do
  mFirstSix=`echo $mLine | cut -c1-6`
  if [ "${mFirstSix}" = "title=" ]; then
    mYear=`sed -n '1p' ${mYearAuthor} | cut -d'=' -f2`
    mAuthor=`sed -n '2p' ${mYearAuthor} | cut -d'=' -f2`
    mOutLine='title='${mYear}${mAuthor}
    sed '1,2d' ${mYearAuthor} > $$Temp
    mv $$Temp ${mYearAuthor}
  else
    mOutLine=${mLine}
  fi
  echo "${mOutLine}"
done < ${mInpFile}
rm -f ${mYearAuthor}
IFS=$OLDIFS


Regards
# 7  
Old 07-28-2007
Quote:
Originally Posted by matrixmadhan
How about combining this, sed and cut to just awk
Code:
mYear=`awk -F"=" 'NR == 1 { print $2 }' filename`

or
Code:
mYear=`awk -F"=" ' { if ( NR == 1 ) { print $2 } else { exit } }' filename`

Matrix, thank you for commenting on my code.

It shows that you read it and tried to understand it.

Here in the site, when I write a code, I do it quickly, most replies take
only a few minutes, with the first solution that comes to my mind, as
the OP is online waiting for an answer -- just making sure it is working
using whatever the OP gave as specification.

If the same problem was given to me as an assignment in my workplace,
with several days to code it, I would take an entire different approach,
which could result in a drastically different script.

Most of the time, for more involved issues, it is not written in shell scripts,
but in a compiled language.

I also have to follow the company standards, place header comments,
have in mind easy of maintenance, etc.

As for changing this specific solution from "sed+cut" to "just awk", it is
possible to do it, as you demonstrated.

It is also possible to do with just "sed", just "perl", just "python", etc.

As the saying goes:
"There are several ways to skin a cat."

Matrix, the great majority of the OPs here are new to UNIX, thus every time
I offer a solution, I keep in mind the following:
1) To keep it easy to read.
2) To keep it simple.
3) To use plain UNIX commands, exception for basic 'sed' commands.

Languages such as 'awk', 'perl', 'python' are very powerful, they are very
useful for people who know how to use them properly, which may take
years for a broad understanding of all its commands and functionality.

Before one start learning any of these languages, he/she must have a
great experience using plain UNIX commands.

A lot of times, the OPs ask for an explanation of the code, even when it
has a single command, such as 'paste' -- most of them don't even know
about 'man' page.

Writing code using advanced UNIX languages may scare an UNIX apprentice
and it may lead him to believe, as a beginner, that he must use this language
to solve his problem.

The other issue is maintenance -- if the solution is presented in advanced
UNIX languages, the OP will have no chance to easily change it.

Thus, my approach to always use plain UNIX commands -- when not possible,
to use 'sed'.

Thanks again for proposing another possible solution for the problem. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove new line starting with a numeric value and append it to the previous line

Hi, i have a file with multiple entries. After some tests with sed i managed to get the file output as follows: lsn=X-LINK-IN0,apc=661:0,state=avail,avail/links=1/1, 00,2110597,2094790,0,81,529,75649011,56435363, lsn=TM1ITP1-AM1ITP1-LS,apc=500:0,state=avail,avail/links=1/1,... (5 Replies)
Discussion started by: nms
5 Replies

2. Shell Programming and Scripting

How to add the line to previous line in | delimited text?

Hi All, I am new to Unix and I have one challenge and below are the details. I have pipe delimited text file in that data has span into multiple lines instead of single line. Sample data. Data should be like below for entire file. 41|216|398555|77|provided complete NP outcome data ... (21 Replies)
Discussion started by: Narasimhasss
21 Replies

3. Shell Programming and Scripting

How to print previous line of multiple pattern matched line?

Hello, I have below format log file, Comparing csv_converted_files/2201/9747.1012H67126.5077292103609547345.csv and csv_converted_files/22019/97447.1012H67126.5077292103609547345.csv Comparing csv_converted_files/2559/9447.1012H67126.5077292103609547345.csv and... (6 Replies)
Discussion started by: arvindshukla81
6 Replies

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

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

6. Shell Programming and Scripting

Replace first field of a line with previous filed of the line

Hi Everyone, I have a file as below: IM2345638,sherfvf,usha,30 IM384940374,deiufbd,usha,30 IM323763822,cdejdkdnbds,theju,15 0,dhejdncbfd,us,20 IM398202038,dhekjdkdld,tj,30 0,foifsjd,u2,40 The output i need is as below IM2345638,sherfvf,usha,30... (4 Replies)
Discussion started by: usha rao
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

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

9. Shell Programming and Scripting

Perl: Conditional replace based on previous and current value in a line

I need to read the contents of a file. Then I need to grep for a keyword and replace part of the grepped line based on the condition of previous and present line. Example input file: V { port1 = P; port2 = 0; shift_port = P0; /* if next shift_port is P0 I need... (9 Replies)
Discussion started by: naveen@
9 Replies

10. 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
Login or Register to Ask a Question