LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-01-2012, 03:09 PM   #1
jakert00
LQ Newbie
 
Registered: Oct 2012
Location: Chicago, IL
Distribution: Ubuntu Server
Posts: 3

Rep: Reputation: Disabled
Script to append filename is not working the way I thought it would


As the title explains I have a script built that appends the filename from every file in the directory, to every line in its respective file, except instead of appending it at the end of the line, it creates a new line to place the ",filename"

Here's the script

for file in *; do echo $file > tempfile; awk '{print $0 "," FILENAME}' $file > tempfile; mv tempfile $file; done

I'm sure its one of those things, it's so simple it's hard.

I appreciate any help or points in the right direction.
 
Old 10-01-2012, 03:13 PM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,133

Rep: Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876
this mite work:
Code:
for file in *
do
 awk -v file=$file '{print $0 "-" file}' $file > $file.mod
 # mv $file.mod $file
done
 
Old 10-01-2012, 03:17 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,516

Rep: Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893
Well I see a couple of issues:

1. By using > for both the echo and the awk, the second will overwrite the first

2. Your erroneous output would suggest the files being viewed are probably written in Windows and hence the line endings are not \n but rather \r\n. You will need to tell awk this or
run something like dos2unix over each file first.

Of course without any actual data to look at this is what the problem sounds like.
 
1 members found this post helpful.
Old 10-01-2012, 04:09 PM   #4
jakert00
LQ Newbie
 
Registered: Oct 2012
Location: Chicago, IL
Distribution: Ubuntu Server
Posts: 3

Original Poster
Rep: Reputation: Disabled
Schneidz, you're code is off a little bit as it overwrites everything in the file with nothing, but I appreciate the input.

grail, no I think you're on to something, the data is pulled from our citrix farm into windows excel. I move it to my Linux box and manipulate it from there. To make awk aware of something like a carriage return can I add an option in my existing code or do I need to write a separate line?
 
Old 10-01-2012, 08:42 PM   #5
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,133

Rep: Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876
^ worx for me. what version awk are you using:
Code:
[schneidz@hyper ~]$ awk --version
GNU Awk 3.1.8
i think your issue mite be that you are trying to redirect the output to the input file (thus overwriting it with nothing). try writing it to a temp output then moving the temp output to the original filename (kinda' like my example).

Last edited by schneidz; 10-02-2012 at 06:29 AM. Reason: typo
 
Old 10-01-2012, 10:39 PM   #6
jakert00
LQ Newbie
 
Registered: Oct 2012
Location: Chicago, IL
Distribution: Ubuntu Server
Posts: 3

Original Poster
Rep: Reputation: Disabled
Smile

Schneidz, Thanks for the help. I was running mawk not gawk. Once I installed gawk the script worked as advertised.
 
Old 10-02-2012, 02:13 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,516

Rep: Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893
You can simply set the RS value to allow for windows based files.
Code:
awk 'BEGIN{RS="[\n\r]+"}{...}'
[COLOR="Silver"]

I would also add that you could perform the globbing within awk itself:
Code:
awk 'BEGIN{RS="[\n\r]+"}{print $0"-"FILENAME >FILENAME".mod";f[FILENAME]}END{for(x in f)print | "mv "x".mod "x}' *

Last edited by grail; 10-02-2012 at 02:19 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Please help script loop into the /tmp/filename.txt out put with filename and wc. dotran Linux - Newbie 10 06-08-2012 05:02 PM
[SOLVED] msync() and NFS isn't working as thought chcarver Programming 3 04-06-2012 04:18 AM
bash script - get filename created by script using variables aolong Linux - General 8 11-19-2009 12:40 PM
Append md5sum or similar to filename, batch copy from multiple directories to one migla Linux - Newbie 5 02-03-2008 05:54 PM
How to append hostname in a script anjaan Programming 5 06-14-2004 10:41 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration