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?
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..
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.
How about combining this, sed and cut to just awk
or
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.
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)
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)
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)
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)
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)
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)
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)
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)
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)