LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-12-2011, 05:24 AM   #1
jmvidalvia
LQ Newbie
 
Registered: Mar 2011
Posts: 23

Rep: Reputation: 0
bash: inserting new line in file if...


Hi,
I don't pretend anyone to do my work, but I am wondering which would be the best way to insert a row into a sorted text file, if the first field changes:

From:
11111,aaaa,bbbb
11111,ccccc,dddd
22222,aaaa,bbbb

To:
11111,aaaa,bbbb
11111,ccccc,dddd
-new text inserted here-
22222,aaaa,bbbb

The condition is that 11111 changed to 22222
(the purpose is adding middrules to a latex input table)
Thanks a lot!
 
Old 03-12-2011, 05:28 AM   #2
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Hello and Welcome to LinuxQuestions,

Is this by any chance homework? It looks like it is. Anyway, have a look at this Sed tutorial, it can be used to do what you need, amongst other tools. LQ users are at their best solving problems but are not in the habit of, like you saying do the work for you. So post what you've tried and we'll take it from there. Enjoy the forums and Linux.

Kind regards,

Eric
 
Old 03-12-2011, 06:06 AM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Could you provide some actual lines of input and desired output, instead of an idea. Using sed and awk the description needs to exact. For example, is the first column always 5 characters in width? If not, I would have had to convert the commas to spaces or tabs for the uniq command to process just the first column.

You could use `uniq' to select changes in the first column. Pipe the results to sed to 1) delete the first line 2) generate a sed script with lines of the form:
/22222,aaaa,bbbb/i\
inserted line
/33333,abcd,defg/i\
inserted line

Then to perform the conversion run the sed script: sed -f inserts.sed input.tex >output.tex
Code:
> cat testfile3
11111,aaaa,bbbb
11111,ccccc,dddd
22222,aaaa,bbbb
22222,cccc,dddd
22222,dddd,eeee
33333,dddd,eeee

> sed -f inserts.sed testfile3
11111,aaaa,bbbb
11111,ccccc,dddd
sample-test
22222,aaaa,bbbb
22222,cccc,dddd
22222,dddd,eeee
sample-test
33333,dddd,eeee
I generated the sed script, testsed3, in a oneliner. The two lines I used could have been in a script, but would be easy to do interactively.

Last edited by jschiwal; 03-12-2011 at 06:16 AM.
 
Old 03-12-2011, 07:25 AM   #4
jmvidalvia
LQ Newbie
 
Registered: Mar 2011
Posts: 23

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by EricTRA View Post
Is this by any chance homework? It looks like it is.
47 years old already!
Old Linux user, but new to this forum

Quote:
Originally Posted by EricTRA View Post
Sed tutorial[/URL], it can be used to do what you need, amongst other tools...
Prefered my old style scripting..

Code:
FILE=1001
NCLI0=0
cat $FILE | while read line; do
NCLI1="${line:0:6}"
if [ "$NCLI1" != "$NCLI0" ];
then
echo "---------------" >> 1002 
NCLI0="$NCLI1"
fi
echo $line >> 1002 
done
Thanks anyhow!
 
Old 03-12-2011, 07:46 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
A simple awk alternative:
Code:
awk -F, '!_[$1]++ {print "---------------"}1' 1001 > 1002
or at least something to tickle your curiosity!
 
Old 03-12-2011, 07:52 AM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
colucix: Your solution is much more concise than mine:
Code:
 sed -f <(uniq -w5 testfile3 | sed '1d;s#.*#/&/i\\\nsample-test#' ) testfile3
 
Old 03-12-2011, 08:37 AM   #7
jmvidalvia
LQ Newbie
 
Registered: Mar 2011
Posts: 23

Original Poster
Rep: Reputation: 0
Just can take my hat off.
How amazing is being able to summarize in just one line of code what I need to do in more than ten!
Thanks to all of you.
 
Old 03-12-2011, 09:21 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
If only looking to add after first change with the awk you may wish to throw a 'NR > 1 &&' at the start
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash : read every line from text file starting at given line number quadmore Programming 4 02-20-2009 12:29 PM
I would like need a suggestion on bash shell : Read a file line by line and do stuff madi3d8 Linux - Newbie 1 01-15-2009 09:30 AM
Problem when inserting a new line in a file by using redirection. philipina Linux - General 2 11-09-2004 01:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration