Append line count to each line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Append line count to each line
# 1  
Old 06-26-2010
Question Append line count to each line

Hello forum,

I need to append the total line count to the end of each line in a file.

The file where this line count needs to be appended is generated by this script:

Quote:
for FILE in *.txt;
do NEWFILE=c$FILE;
cut -f1 $FILE | sort | uniq -c | sort -k1,1nr -k2 | sed ${1:-20}q | sed 's/^ *//' > $NEWFILE;
done
The script does a word frequency count by the first column of a file.

if I add wc -l at the end then the line count appears only in the last line. But I want the line count to appear at the end of each line.

Thanks!
# 2  
Old 06-26-2010
Hi
Provide a sample input file and an expected output file.

Guru.
# 3  
Old 06-26-2010
Sample input:

aaa bbb ccc
aaa bbb ccc
ddd eee fff
ddd eee fff

The output from my script will be:
2 aaa
2 ddd

I want the sum of occurrence of aaa and bbb =2+2 i.e. the line count to show up at the end of each line

i.e. output will be:

2 aaa 4
2 ddd 4
# 4  
Old 06-26-2010
Hi

Code:
awk '{a[$1]++; j++}END{ for (i in a) print a[i],i,j;}' infile

Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 5  
Old 06-26-2010
Thanks that works fine except that the sorting by the number of occurrences of the words is not there. I appreciate your help.

---------- Post updated at 12:43 AM ---------- Previous update was at 12:25 AM ----------

ok..i got it now...thanks
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

Count the pipes "|" in line and delete line if count greter then number.

Hello, I have been working on Awk/sed one liner which counts the number of occurrences of '|' in pipe separated lines of file and delete the line from files if count exceeds "17". i.e need to get records having exact 17 pipe separated fields(no more or less) currently i have below : awk... (1 Reply)
Discussion started by: ketanraut
1 Replies

3. Shell Programming and Scripting

Append Next line with current Line bassed on condition

Hi, I have an XML file and I am tring to extract some data form it, after lot of data cleaning process, I ended up with an issue, and need your urgent support. my current input data in below format: <Node>xxxxxx <Node>yyyyy</Node> <Node>zzzzzz <Node>12345</node> I need... (9 Replies)
Discussion started by: rramkrishnas
9 Replies

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

5. Shell Programming and Scripting

find a certain line and append text to the end of the line

After I create printer queues in AIX, I have to append a filter file location within that printers custom file. within lets say test_queue.txt I need to find the row that starts with :699 and then I need to append on the end the string /usr/local/bin/k_portrait.sh. Now I've gotten the sed... (2 Replies)
Discussion started by: peachclift
2 Replies

6. Shell Programming and Scripting

Append next line to previous line when one pattern not found

Hi, I need help for below scenario.I have a flat file which is having records seperated by delimiters which will represent each record for oracle table.My Control file will consider each line as one record for that table. Some of the lines are aligned in two/three lines so that records are... (4 Replies)
Discussion started by: kannansr621
4 Replies

7. Shell Programming and Scripting

append delimeter count for each line in text file

Hi guys, plz tell me how to achieve this how to delete the lines in a file using sed command (6 Replies)
Discussion started by: hari908
6 Replies

8. Shell Programming and Scripting

Append instance count to each line

Hello forum, I need help with a script for displaying the number of instances/times a particular line appears in a tab-delimited file and append that number to the end of the line. Example text file: aaa bbb ccc ddd ggg hhh kkk nnn aaa bbb ccc ddd aaa bbb ccc ddd ppp qqq nnn sss ggg... (1 Reply)
Discussion started by: jaysean
1 Replies

9. Shell Programming and Scripting

Joining lines in reverse. append line 1 to line 2.

Hi I have used many times the various methods to append two lines together in a file. This time I want to append the 1st line to the second and repeat for the complete file.... an example This is the file owns the big brown dog joe owns the small black dog jim What I want is ... (7 Replies)
Discussion started by: dwalley
7 Replies

10. Shell Programming and Scripting

Line Count and Append it to the end of the file.

Hi, I want to get a Line count of a file and append that at the end of the file. The Line count should not include the Headers : ------------------ COL1,COL2,COL3 123,abc,011 111,abd,0212 Record Count: 2 ------------------- Thanks. (7 Replies)
Discussion started by: smc3
7 Replies
Login or Register to Ask a Question