Visit Our UNIX and Linux User Community


Help me in getting the desired result


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help me in getting the desired result
# 1  
Old 12-18-2012
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 05:50 AM.. Reason: Scott - Thread moved, radoulov - code tags.
# 2  
Old 12-18-2012
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  
Old 12-18-2012
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  
Old 12-18-2012
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  
Old 12-18-2012
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  
Old 12-18-2012
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  
Old 12-18-2012
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
Test Your Knowledge in Computers #667
Difficulty: Easy
IEEE 802 is a family of IEEE standards dealing with local area networks and metropolitan area networks.
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. UNIX for Dummies Questions & Answers

if statement not working as desired

Hello all, I am trying to write a post-commit hook script using bash script. What I am trying to do here is: Developers check in their files to a branch. I check the repository and based on the commit I email QA people. QA verifies and moves the files to a prod branch and email is sent... (1 Reply)
Discussion started by: kminkeller
1 Replies

6. Shell Programming and Scripting

Printing out desired content

I want this script to read notes and print the contents of any file that starts with messages and is possibly followed by a period and a digit. I've made this code, but how do I make it so that IF there is a period and a digit. Right now it only prints out the result if it is for example;... (8 Replies)
Discussion started by: HardyV2
8 Replies

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

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