Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Add filenames to top of each files in a directory

Shell Programming and Scripting


Tags
while read loop sed do filename add top

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 05-18-2017   -   Original Discussion by baris35
baris35 baris35 is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 30 September 2017, 1:50 PM EDT
Posts: 122
Thanks: 54
Thanked 5 Times in 5 Posts
[sorted out] Add filenames to top of each files in a directory

Hello,

I tried different solutions given in various linux portals but no luck..

The directory consists of files with no-extension.
Each file has only one line. I need to add each filename to the top of each file so eventually each file will have two rows.
Filenames have spaces between words and some files have multiple spaces at the end of their names.


At first I tried this one:


Code:
while read F ; do
grep sed  -i '1i $F' $F > $F.txt
done


Also below one is not working:



Code:
for filename in $(ls $*)
do
	sed "1s/^/${filename} \n/" ${filename} > $filename.txt 
	echo Done ${filename} 
done

sleep 2


Any help will be appreciated

Kind regards
Boris

Last edited by baris35; 05-18-2017 at 01:08 PM.. Reason: [sorted out]
Sponsored Links
    #2  
Old Unix and Linux 05-18-2017   -   Original Discussion by baris35
RavinderSingh13 RavinderSingh13 is offline Forum Advisor  
Registered User
 
Join Date: May 2013
Last Activity: 18 November 2017, 9:36 AM EST
Location: Chennai
Posts: 2,670
Thanks: 588
Thanked 1,272 Times in 1,145 Posts
Hello baris35,

Could you please try following(I haven't tested it though).

Code:
for file in *
do
    awk 'NR==1{print FILENAME} 1' $file > $file".tmp"
    mv $file".tmp" $file
done

Thanks,
R. Singh
The Following User Says Thank You to RavinderSingh13 For This Useful Post:
baris35 (05-18-2017)
Sponsored Links
    #3  
Old Unix and Linux 05-18-2017   -   Original Discussion by baris35
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 17 November 2017, 9:25 PM EST
Location: Saskatchewan
Posts: 22,512
Thanks: 1,151
Thanked 4,273 Times in 3,946 Posts
for FILE in `ls *` is a dangerous use of backticks and pointless besides, don't do that. * does not need ls or any other external command's help to work.

You can do this with a subshell, which first prints the file name, then reads out the file contents, with the entire subshell's output dumped into "$FILE".txt


Code:
for FILE in *
do
        ( echo "$FILE" ; cat "$FILE" ) < "$FILE" > "$FILE".txt
done

The Following User Says Thank You to Corona688 For This Useful Post:
baris35 (05-18-2017)
    #4  
Old Unix and Linux 05-18-2017   -   Original Discussion by baris35
baris35 baris35 is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 30 September 2017, 1:50 PM EDT
Posts: 122
Thanks: 54
Thanked 5 Times in 5 Posts
Hello Ravinder,
Thanks for your answer,
it gives error for some reason:


Code:
line 5: $file".tmp": ambiguous redirect

Hello Corona,
Thanks for the answer, that is working as expected.


Many Thanks!
Boris
Sponsored Links
    #5  
Old Unix and Linux 05-18-2017   -   Original Discussion by baris35
RavinderSingh13 RavinderSingh13 is offline Forum Advisor  
Registered User
 
Join Date: May 2013
Last Activity: 18 November 2017, 9:36 AM EST
Location: Chennai
Posts: 2,670
Thanks: 588
Thanked 1,272 Times in 1,145 Posts
Quote:
Originally Posted by baris35 View Post
Hello Ravinder,
Thanks for your answer,
it gives error for some reason:

Code:
line 5: $file".tmp": ambiguous redirect

Hello Corona,
Thanks for the answer, that is working as expected.
Many Thanks!
Boris
Hello baris35,

Not sure about it, could you please check following and let me know if this helps you.(I tested now previous code and it worked fine for me)

Code:
for file in *
do
    awk 'NR==1{print FILENAME} 1' $file > "$file.tmp"
    mv "$file.tmp" $file
done

Thanks,
R. Singh
Sponsored Links
    #6  
Old Unix and Linux 05-18-2017   -   Original Discussion by baris35
baris35 baris35 is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 30 September 2017, 1:50 PM EDT
Posts: 122
Thanks: 54
Thanked 5 Times in 5 Posts
Hello Ravinder,

Here is the output:


Code:
awk: cannot open #EXTINF:0,DE: (No such file or directory)
mv: target ‘CY’ is not a directory
awk: cannot open #EXTINF:0,DE: (No such file or directory)
mv: target ‘.txt’ is not a directory
awk: cannot open #EXTINF:0,DE: (No such file or directory)
mv: target ‘.txt’ is not a directory

Kind regards
Boris
Sponsored Links
    #7  
Old Unix and Linux 05-18-2017   -   Original Discussion by baris35
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 17 November 2017, 9:25 PM EST
Location: Saskatchewan
Posts: 22,512
Thanks: 1,151
Thanked 4,273 Times in 3,946 Posts
You cannot possibly have been running the code he gave you.

Show exactly what you did please, word for word, letter for letter, keystroke for keystroke.
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How do I custom sort the files in a directory using the filenames in a text file.? KMusunuru UNIX for Beginners Questions & Answers 5 02-15-2017 09:49 PM
There are multiple filenames in the directory.How to return the the lastest files for each file name grand_sam Shell Programming and Scripting 4 02-18-2015 11:08 PM
Change all filenames in a directory crumb Shell Programming and Scripting 17 09-18-2009 09:30 PM
concatenating the filenames in a directory nvuradi Shell Programming and Scripting 7 11-12-2008 06:23 PM
looping thru filenames in a directory silas.john Shell Programming and Scripting 1 07-02-2008 09:27 AM



All times are GMT -4. The time now is 11:59 AM.