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
# 8  
Old 12-18-2012
Quote:
Originally Posted by PikK45
Code:
awk -F"|" 'NR==FNR{a=NR;next}{print $0""a"|"}' file file

First of all your sample file has only 3 lines so final NR=3.
I am not getting what is the problem you are getting.
And you don't need -F"|" for adding extra field at the end of the line.

Code:
awk 'NR==FNR{a=NR;next}{print $0""a"|"}' file file
1234|1|12|14|1|1|2012-11-16 06:45:34|test|1|0|this is a test|3|
1234|1|12|15|4|8|2012-11-16 06:45:34|test|1|0|try your best. 1234|3|
1234|1|12|16|2|5|2012-11-16 06:45:34|test|1|0|Bad Bad worst|3|

and if you want to print number of fields then use NF.

Code:
$ awk -F"|" '{print $0""NF"|"}' file
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|

# 9  
Old 12-18-2012
Basically I was trying to add the number of lines in the file at the end of each line!

Since this is troubling me a little, I used
Code:
sed "s/$/`cat file | wc -l`\|/g" file > file1

# 10  
Old 12-18-2012
Quote:
Originally Posted by PikK45
Basically I was trying to add the number of lines in the file at the end of each line!

Since this is troubling me a little, I used
Code:
sed "s/$/`cat file | wc -l`\|/g" file > file1

Use variable instead of this..

Code:
var=$(cat file | wc -l )
sed "s/$/$var\|/g" file

 
Login or Register to Ask a Question

Previous Thread | Next Thread

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
Login or Register to Ask a Question