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

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 1 Week Ago
baris35 baris35 is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 25 May 2017, 9:02 AM EDT
Posts: 98
Thanks: 43
Thanked 4 Times in 4 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; 1 Week Ago at 12:08 PM.. Reason: [sorted out]
Sponsored Links
    #2  
Old Unix and Linux 1 Week Ago
RavinderSingh13 RavinderSingh13 is online now Forum Advisor  
Registered User
 
Join Date: May 2013
Last Activity: 29 May 2017, 7:35 AM EDT
Location: Chennai
Posts: 2,477
Thanks: 553
Thanked 1,172 Times in 1,055 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 (1 Week Ago)
Sponsored Links
    #3  
Old Unix and Linux 1 Week Ago
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 26 May 2017, 4:17 PM EDT
Location: Saskatchewan
Posts: 22,183
Thanks: 1,092
Thanked 4,174 Times in 3,861 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 (1 Week Ago)
    #4  
Old Unix and Linux 1 Week Ago
baris35 baris35 is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 25 May 2017, 9:02 AM EDT
Posts: 98
Thanks: 43
Thanked 4 Times in 4 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 1 Week Ago
RavinderSingh13 RavinderSingh13 is online now Forum Advisor  
Registered User
 
Join Date: May 2013
Last Activity: 29 May 2017, 7:35 AM EDT
Location: Chennai
Posts: 2,477
Thanks: 553
Thanked 1,172 Times in 1,055 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 1 Week Ago
baris35 baris35 is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 25 May 2017, 9:02 AM EDT
Posts: 98
Thanks: 43
Thanked 4 Times in 4 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 1 Week Ago
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 26 May 2017, 4:17 PM EDT
Location: Saskatchewan
Posts: 22,183
Thanks: 1,092
Thanked 4,174 Times in 3,861 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
Reply

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 08: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 10:08 PM
Change all filenames in a directory crumb Shell Programming and Scripting 17 09-18-2009 08:30 PM
concatenating the filenames in a directory nvuradi Shell Programming and Scripting 7 11-12-2008 05:23 PM
looping thru filenames in a directory silas.john Shell Programming and Scripting 1 07-02-2008 08:27 AM



All times are GMT -4. The time now is 07:45 AM.