LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   bash: inserting new line in file if... (https://www.linuxquestions.org/questions/programming-9/bash-inserting-new-line-in-file-if-868098/)

jmvidalvia 03-12-2011 05:24 AM

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!

EricTRA 03-12-2011 05:28 AM

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

jschiwal 03-12-2011 06:06 AM

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.

jmvidalvia 03-12-2011 07:25 AM

Quote:

Originally Posted by EricTRA (Post 4287929)
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 (Post 4287929)
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!

colucix 03-12-2011 07:46 AM

A simple awk alternative:
Code:

awk -F, '!_[$1]++ {print "---------------"}1' 1001 > 1002
or at least something to tickle your curiosity! :)

jschiwal 03-12-2011 07:52 AM

colucix: Your solution is much more concise than mine:
Code:

sed -f <(uniq -w5 testfile3 | sed '1d;s#.*#/&/i\\\nsample-test#' ) testfile3

jmvidalvia 03-12-2011 08:37 AM

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.

grail 03-12-2011 09:21 AM

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


All times are GMT -5. The time now is 08:47 PM.