Print file end


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print file end
# 1  
Old 06-29-2012
Print file end

Hello All,

I have file names in a directory that ends this way

File Names in the directory

Code:
abc.log.1.txt
abc.log.2.txt
abc.log.3.txt

I want to print the 1, 2 and 3 in the file names as the first column in my output, and some contents of the files as second and fourth columns. I wrote this for loop, but for some reasons it is not working. Can someone help me?

Code:
for i in `ls /path/`; do awk '{f='ls $i | awk -F. '{print $3}'; if($1 ~ /^SOME/){print f"\t"$5"\t""0""\t"$6}}' OFS="\t" $i; done | head

The error I am saying is

Code:
-bash: syntax error near unexpected token `{print'

Contents of abc.log.1.txt

Code:
SOME HN 098.12 HN kill 09876

Contents of abc.log.2.txt
Code:
SOME HJ 98.76 HJ mill 12345

My final output would be
Code:
#chromosome	#header2 header3 header4
1 kill 0 09876
2 mill 0 12345

The zero in the 3rd column is common for all the output records and the header is same.

Thanks in advance
# 2  
Old 06-29-2012
That's a dangerous use of backticks -- spaces in filenames, too many files, and other such conditions may cause your code to break.

That's also a useless use of ls. There's much better ways to get a variable into awk, and you certainly don't need to use ls twice to list one file.

Your awk problem is caused by a misplaced quote in the middle, causing the awk program to end early and feed the rest into BASH.

I'd try this:

Code:
awk '{split(FILENAME, A, ".");  print A[3], $5, 0, $6 }' /path/*

If that complains there are too many files:

Code:
ls /path/ | xargs awk '{split(FILENAME, A, ".");  print A[3], $5, 0, $6 }'

FILENAME is a special variable inside awk meaning 'the current file being processed'. I split it apart on the "." and print the third item found in it along with the columns you want.
# 3  
Old 07-01-2012
Quote:
Originally Posted by Corona688
That's a dangerous use of backticks -- spaces in filenames, too many files, and other such conditions may cause your code to break.

That's also a useless use of ls. There's much better ways to get a variable into awk, and you certainly don't need to use ls twice to list one file.

Your awk problem is caused by a misplaced quote in the middle, causing the awk program to end early and feed the rest into BASH.

I'd try this:

Code:
awk '{split(FILENAME, A, ".");  print A[3], $5, 0, $6 }' /path/*

If that complains there are too many files:

Code:
ls /path/ | xargs awk '{split(FILENAME, A, ".");  print A[3], $5, 0, $6 }'

FILENAME is a special variable inside awk meaning 'the current file being processed'. I split it apart on the "." and print the third item found in it along with the columns you want.

Corona,

Thanks for your detailed information. How do I add the header?
# 4  
Old 07-03-2012
Code:
echo "header" > filename
awk '...' >> filename

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print one sentence 40 to 50 words end with period in a file

Hi All, Is there another way to achieve this? how get short phrase in a sentence with character count of 100 to 155 words end with period but don't end something like 50,000. . Here's my current script but the output is not good. This will use for my snippets or preview. grep... (6 Replies)
Discussion started by: lxdorney
6 Replies

2. Shell Programming and Scripting

UNIX help to print 50 lines after every 3rd occurrence pattern till end of file

I need help with extract/print lines till stop pattern. This needs to happen after every 3rd occurrence of start pattern and continue till end of file. Consider below is an example of the log file. my start pattern will be every 3rd occurrence of ERROR_FILE_NOT_FOUND and stop pattern will be... (5 Replies)
Discussion started by: NSS
5 Replies

3. UNIX for Dummies Questions & Answers

Get Notification at end of print

Hi everybody, i want to know if a print was well done and if it's finished. How can i do that ? Must i do something in /etc/printcap ? Thanks in advance :) (2 Replies)
Discussion started by: shibien
2 Replies

4. Shell Programming and Scripting

awk one liner to print to end of line

Given: 1,2,whatever,a,940,sot how can i print from one particular field to the end of line? awk -F"," '{print $2 - endofline}' the delimiter just happens to be a comma "," in this case. in other cases, it could be hypens: 1---2---whatever---a---940---sot (4 Replies)
Discussion started by: SkySmart
4 Replies

5. Shell Programming and Scripting

sed print from last occurrence match until the end of file

Hi, i have file f1.txt with data like: CHECK a b CHECK c d CHECK e f JOB_START .... I want to match the last occurrence of 'CHECK' until the end of the file. I can use awk: awk '/^CHECK/ { buf = "" } { buf = buf "\n" $0 } END { print buf }' f1.txt | tail +2Is there a cleaner way of... (2 Replies)
Discussion started by: ysrini
2 Replies

6. Shell Programming and Scripting

Print required values at end of the file by using AWK

I am looking help in awk, quick overview. we will get feed from external system . The input file looks like below. Detail Id Info Id Order Id STATUS Status Date FileDetail 99127942 819718 CMOG223481502 PR 04-17-2011 06:01:34PM... (7 Replies)
Discussion started by: dvrbabu
7 Replies

7. Shell Programming and Scripting

How to Print from matching word to end using awk

Team, Could some one help me in Printing from matching word to end using awk For ex: Input: I am tester for now I am tester yesterday I am tester tomorrow O/p tester for now tester yesterday tester tomorrow i.e Starting from tester till end of sentence (5 Replies)
Discussion started by: mallak
5 Replies

8. Shell Programming and Scripting

print string at the end of lines in text file

hello, I go text file like this E:/DDD/Dyndede/wwww E:/DDD/sss.com/ffffg/fff E:/DDD/vvvvvv/dd E:/DDD/sss.com/bbbbbb E:/DDD/sss.com/nnnn/xxI want to print /alpha.jpg at the end of every lines like that E:/DDD/Dyndede/wwww/alpha.jpg E:/DDD/sss.com/ffffg/fff/alpha.jpg... (8 Replies)
Discussion started by: davidkhan
8 Replies

9. UNIX and Linux Applications

Print date at END clause of AWK

Hi to all! I 'm new in unix programing so... may be I decided a wrong tool to solve the problem but anyway... all road goes to rome jajaja. My question is: There is any way to print date at the END clause of an AWK script. I mean, I'm writing a tool with AWK and the results are redirected to a... (4 Replies)
Discussion started by: fmeriles
4 Replies

10. Shell Programming and Scripting

Print starting 3rd line until end of the file.

Hi, I want to Print starting 3rd line until end of the file. Pls let me know the command. Thanks in advance. (1 Reply)
Discussion started by: smc3
1 Replies
Login or Register to Ask a Question