Help me in getting the desired result


 
Thread Tools Search this Thread
# 1  
Help me in getting the desired result

Hi All,

I have input file which is pipe delimited
Code:
A  | 1
B  | 2
c  | 3

I need output of displaying total number of lines in a file as last column.

Code:
A | 1 | 3-----total number of linesin file.
B | 2 | 3
c | 3 | 3


Last edited by radoulov; 12-18-2012 at 06:50 AM.. Reason: Scott - Thread moved, radoulov - code tags.
# 2  
I think you have posted this post in wrong thread.

below are two methods to do your work..

Code:
a=$( wc -l file | cut -f1 -d " ")
awk -v var="$a" '{ print $0" | "var}' file

Code:
awk 'NR==FNR{a=NR;next}{print $0" | "a}' file file

# 3  
Code:
awk 'NR==FNR{a=NR;next}{print $0" | "a}' file file

Pamu,
For the above code, consider a file with 50 fields. Do you think it'd work?
# 4  
Quote:
Originally Posted by PikK45
Code:
awk 'NR==FNR{a=NR;next}{print $0" | "a}' file file

Pamu,
For the above code, consider a file with 50 fields. Do you think it'd work?
Hi PikK45,

Yes, sure it will work.

$0 indicates whole line, means all your 50 fields. Smilie
# 5  
I tried with an example file that contains 25 fields, separated by "|".

There are text fields which has spaces as well. Thus I added
Code:
-F"|"

to the awk line that you gave. It didn't work as expected!
# 6  
Quote:
Originally Posted by PikK45
I tried with an example file that contains 25 fields, separated by "|".

There are text fields which has spaces as well. Thus I added
Code:
-F"|"

to the awk line that you gave. It didn't work as expected!
Could you please post your sample input and desired output.
# 7  
File:
Code:
1234|1|12|14|1|1|2012-11-16 06:45:34|test|1|0|this is a test|
1234|1|12|15|4|8|2012-11-16 06:45:34|test|1|0|try your best. 1234|
1234|1|12|16|2|5|2012-11-16 06:45:34|test|1|0|Bad Bad worst|

Code:
awk -F"|" 'NR==FNR{a=NR;next}{print $0""a"|"}' file file

Output:
Code:
1234|1|12|14|1|1|2012-11-16 06:45:34|test|1|0|this is a test|12|
1234|1|12|15|4|8|2012-11-16 06:45:34|test|1|0|try your best. 1234|12|
1234|1|12|16|2|5|2012-11-16 06:45:34|test|1|0|Bad Bad worst|12|

 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #295
Difficulty: Easy
Ubuntu releases updated versions predictably every 3 months.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to compare the current result with previous line result.?

Hi Gurus, I have requirement to compare current result with previous reuslt. The sample case is below. 1 job1 1 1 job2 2 1 job3 3 2 job_a1 1 2 job_a2 2 2 job_a3 3 3 job_b1 1 3 job_b2 2 for above sample file, GID is group ID, for input line, the job run... (1 Reply)
Discussion started by: ken6503
1 Replies

2. Shell Programming and Scripting

awk split command to get the desired result

Dear all, I am using the awk 'split' command to get the particular value. FILE=InputFile_009_0.txt Temp=$(echo $FILE | awk '{split($FILE, a, "e_"); print a}') I would like to have the Temp take the value as : _009_0 ... (4 Replies)
Discussion started by: emily
4 Replies

3. Shell Programming and Scripting

Print if found non-desired result

I have a result like this root@server # grep -rl maldet /etc/cron* /etc/cron.d/maldet_daily /etc/cron.d/malcron /etc/cron.d/malcrondaily /etc/cron.d/malcronweekly What I need is, I need an if/else condition such that, if there is any output other than /etc/cron.d/maldet_daily in the... (8 Replies)
Discussion started by: anil510
8 Replies

4. Shell Programming and Scripting

need to get the desired output

Below is the my cide which is working fine but I am not getting the output indesired format.there is some problem in alignment.Can someone help me to correct this? if ]; then summary=$( echo -e "Please review the log file of auto coloclean utility.\n"; echo -e... (2 Replies)
Discussion started by: anuragpgtgerman
2 Replies

5. Shell Programming and Scripting

how to get desired output after redirection

hi i am running script which contains the commmnds and i am redirecting the script output to a file. like ./script 1> result.txt 2>&1 the above redirection is not working for commands when run in background in a script. but the problem here result.txt containg output which is repeated.... (3 Replies)
Discussion started by: raji
3 Replies

6. UNIX for Dummies Questions & Answers

display the result of wc -l with words before and after the result

hello showrev -p | wc -l returns: 381 What to do in case I want to have this output: number of lines returned by showrev -p is: 381 thx (3 Replies)
Discussion started by: melanie_pfefer
3 Replies

7. UNIX for Dummies Questions & Answers

chmod not working as desired

my file had permission -rw-rw-r-- I did chmod +rwx, expecting everything to now be rwx, but it is -rwxrwxr-x why doesn't o have x permission? thanks. (2 Replies)
Discussion started by: JamesByars
2 Replies

8. HP-UX

Desired Format !

Hi everybody, I just need desired ouput from text file which should have folowing format; "2007-06-25 00:03:32.926+05:30",12354369,"Load","Completed","Rs.-5,556.00",9452217714 "2007-06-25 00:06:57.357+05:30",12354371,"Load","Completed","Rs.-56.00",9415766266 "2007-06-25... (1 Reply)
Discussion started by: prasanth_babu
1 Replies

9. Shell Programming and Scripting

Outputting formatted Result log file from old 30000 lines result log<help required>

Well I have a 3000 lines result log file that contains all the machine data when it does the testing... It has 3 different section that i am intrsted in 1) starting with "20071126 11:11:11 Machine Header 1" 1000 lines... "End machine header 1" 2) starting with "20071126 12:12:12 Machine... (5 Replies)
Discussion started by: vikas.iet
5 Replies

10. Shell Programming and Scripting

Help me in getting the desired output

I wanted to put "|" this sign at starting and at end of every field but its not working with first field like Currently the out put is : abc | abc | abc | xyz | xyz | xyz | But I want the out put in this form: | abc | abc | abc | | xyz | xyz | xyz | plz help me. (2 Replies)
Discussion started by: akash
2 Replies

Featured Tech Videos