sed or awk help - line numbering w/ different start value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed or awk help - line numbering w/ different start value
# 1  
Old 03-07-2009
sed or awk help - line numbering w/ different start value

I'm pretty new to sed and awk, and I can't quite figure this one out. I've been trying with sed, as I'm more comfortable with it for the time being, but any tool that fits the bill will be fine.

I have a few files, whose contents appear more or less like so:

1|True|12094856|12094856|Test|
2|True|12209485|12209485|Production|
3|True|13056733|13056773|Test|

The problem is they all start at 1, and I'd like to merge them, starting where the last one left off. For example, the first file has 56 entries, so when I merge another one with it, i'd like it to start at 57 instead of 1. Any ideas?
# 2  
Old 03-07-2009
Please post a relevant sample of your input data and an example of the desired output.
# 3  
Old 03-07-2009
replace the first field with a counter in awk

concat the files together, and replace the first field with a counter in awk, something like:


#!/usr/bin/ksh
for file in file1.txt file2.txt file3.txt;do
cat $file >> file.new
done

awk -F\| '
BEGIN {
OFS="|"
}
{
$1=NR
print
}
' file.new
# 4  
Old 03-07-2009
@radoulov I have a master file, master.txt, with about 50 lines like the following:

1|True|12094856|12094856|Test|
2|True|12209485|12209485|Production|
3|True|13056733|13056773|Test|
...
56|True|38077408|38077408|Production
I also have 3 other, shorter files that I want appended to the master file, continuing the count in the first column where it ends in the master page, so tmpFile1 has this content:

1|True|87849057|87849057|Test|
2|False|103994858|103994858|Production|
3|True|88345576|88345576|Production|
...

Now the master file should have this when finished:

1|True|12094856|12094856|Test|
2|True|12209485|12209485|Production|
3|True|13056733|13056773|Test|
...
56|True|38077408|38077408|Production
57|True|87849057|87849057|Test|
58|False|103994858|103994858|Production|
59|True|88345576|88345576|Production|
...

@TinWalrus I was thinking about the problem this morning, and came to a similar conclusion (though I didn't come up with the code,as awk is completely new to me, so thank you for that). It isn'[t quite the elegant answer I was looking for, but barring a better solution I can make do with that. The issue is that I will be doing this again from time to time, and would prefer appending the new files to the master instead of renumbering them all the time, so the trick I can't figure out is how to tell awk (or sed) to start numbering at a different value than one.
# 5  
Old 03-07-2009
Should be something like this:

Code:
awk -F"|" '{$1=NR}1' OFS="|" file1 file2 > file3

Regards
# 6  
Old 03-07-2009
And if for whatever reason $1 does not correspond to NR:

Code:
awk > __temp__ -F\|  '
NR == FNR { l = $1; print; next }
$1 = ++l' OFS=\| master temp  &&
mv __temp__ master

Be aware that the code above will overwrite your master file,
so back it up before trying the command.
# 7  
Old 03-07-2009
Thanks for all the help guys (and gals on the off chance that it applies). Both of these last 2 solutions are workable, though radoulov's is closest to what I intended. This will make my life much easier.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing string from last field of the nth line of file to start (or end) of each line (awk I think)

My file (the output of an experiment) starts off looking like this, _____________________________________________________________ Subjects incorporated to date: 001 Data file started on machine PKSHS260-05CP ********************************************************************** Subject 1,... (9 Replies)
Discussion started by: samonl
9 Replies

2. Shell Programming and Scripting

awk use sequential line numbering in output

The awk below produces an output with the original header and only the matching lines (which is good), but the output where the original line numbering in the match found on is used. I can not figure out how to sequentially number the output instead of using the original. I did try to add... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

awk to count start and end keyword in a line

Hello fellow awkers and seders: need to figure out a way to ensure a software deployment has completed by checking its trace file in which I can store the deployment results as follows: echo $testvar ===== Summary - Deploy Result - Start ===== ===== Summary - Deploy Result - End =====... (1 Reply)
Discussion started by: ux4me
1 Replies

4. Shell Programming and Scripting

start line numbering with arbitrary number?

Hi, I want to do the following: Extract some lines from different files and copy them into one file, with the first column being the line number. I do this with cat file1 file2 file3 |grep 'xxx' |nl > output.file Works fine. But if I want to add data of interest from a fourth file to the... (2 Replies)
Discussion started by: paracetamol
2 Replies

5. Shell Programming and Scripting

sed replace values of column with ordered numbering

Hello, I am trying to write a shell script that will create a gnuplot file. My main problem is that I have a data file with two columns: 1.05929120E+09 5.0000701214792 1.05930096E+09 5.00006386985764 1.05930584E+09 5.00019465404908 1.05931072E+09 5.00031960589719 ... (2 Replies)
Discussion started by: pau
2 Replies

6. Shell Programming and Scripting

SED Question: Search and Replace start of line to matching pattern

Hi guys, got a problem here with sed on the command line. If i have a string as below: online xx:wer:xcv: sdf:/asdf/http:https-asdfd How can i match the pattern "http:" and replace the start of the string to the pattern with null? I tried the following but it doesn't work: ... (3 Replies)
Discussion started by: DrivesMeCrazy
3 Replies

7. UNIX for Advanced & Expert Users

Sed - add text to start of line if 1st char anything but space

Problem: I have a lot of files, the files first line should always have 4 spaces before any text. Occasionally some of the files will miss the leading spaces and it's a problem. This is only in the first line. So if there are 4 spaces then text, do nothing. If there are not 4 spaces, add 4... (2 Replies)
Discussion started by: Vryali
2 Replies

8. Shell Programming and Scripting

numbering each line in a text file

I have a simple text file. I want to number each line in that file . for example: My text file is unix my file test My output should be 1 unix 2 my file 3 test (5 Replies)
Discussion started by: pitagi
5 Replies

9. Shell Programming and Scripting

sed remove last 10 characters of a line start from 3rd line

hello experts, I need a sed command that remove last 10 characters of a line start from 3rd line. any suggestions? Thanks you (7 Replies)
Discussion started by: minifish
7 Replies

10. Shell Programming and Scripting

AWK Multi-Line Records Numbering Problem

I have a set of files of multi-line records with the records separated by a blank line. I needed to add a record number to the front of each line followed by a colon and did the following: awk 'BEGIN {FS = "\n"; RS = ""}{for (i=1; i<=NF; i++)print NR,":",$i}' ~/Desktop/data98-1-25.txt >... (3 Replies)
Discussion started by: RacerX
3 Replies
Login or Register to Ask a Question