LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 05-30-2009, 06:34 PM   #1
allele
Member
 
Registered: May 2009
Posts: 42

Rep: Reputation: 16
append to end of each file -- -bash: *: ambiguous redirect


I have a folder containing about 150 files. I want to append three returns to the end of each one (and prepend a tab, but that method should be similar to this). I type three returns into a new file and try:

$ cat returnsfile >> *
-bash: *: ambiguous redirect

I think bash is telling me it doesn't know which file I want to append to. How do I tell it I want to append to all of them?

I am getting the syntax wrong because if I do:

$ cat returnsfile >> fileone filetwo

then filetwo isn't edited and fileone is edited to:

Code:
text of fileone


text of filetwo
 
Old 05-30-2009, 06:55 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Use a loop:
Code:
for file in *
do
  cat returnsfile >> $file
done
Note that this will try to append returnsfile to itself at some point and you will get an error like "cat: returnsfile: input file is output file". To avoid this, you can simply put returnsfile in another directory. In alternative you can use sed to append three newlines:
Code:
sed -i 's/$/\n\n\n/' $file
 
Old 05-30-2009, 08:23 PM   #3
allele
Member
 
Registered: May 2009
Posts: 42

Original Poster
Rep: Reputation: 16
For the first, I get

../script: line 4: $file: ambiguous redirect

For the second (putting that line in the loop from the first instead of cat) I get

sed: 1: "one": invalid command code o
sed: -i may not be used with stdin
sed: 1: "two": undefined label 'wo'

The one and two are the names of the files I'm testing this on.
 
Old 05-31-2009, 02:14 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
The error "sed -i may not be used with standard in" means that the command has not been used as I suggested, since if $file is the actual name of a file, sed must modify that file, not some unspecified standard input. Regarding the error $file: ambiguous redirect, again if $file would be a real file name there is no reason for that. Please, can you post the content of the script and the test you did?

Also, I apologize but the sed command does not append three newlines at the end of the file, but at the end of every line. The correct form is:
Code:
sed -i '$a\\n\n' $file
where $ in this case is the address for "the end of the file".
 
Old 06-01-2009, 09:50 AM   #5
allele
Member
 
Registered: May 2009
Posts: 42

Original Poster
Rep: Reputation: 16
I am quite clueless so am sorry if I am doing something totally wrong, but I saved as a file:

Code:
#! /bin/sh

for file in *
do
sed -i '$a\\n\n' $file
done
made a directory
$ ls tst
one.txt two.txt

$ bash ../sedscript
sed: 1: "one.txt": invalid command code o
sed: 1: "two.txt": undefined label 'wo.txt'

It didn't say
sed: -i may not be used with stdin
this time, so I must have done something differently this time, but I'm not sure what. It doesn't edit the files.

The script
Code:
#! /bin/sh
for file in *
do
cat ../returnsfile >> $file
done
works with the test folder, but not the folder of files I actually want to edit, where it returns many identical errors:
$ bash script
../script: line 4: $file: ambiguous redirect
(lots of these)
 
Old 06-01-2009, 09:57 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Maybe it depends on the actual content of the directory, that is on how the wildcard is expanded. Try to run the script as:
Code:
bash -x scriptname
and post the result (or the first lines of the result until the first error). This gives a trace of the actual commands executed by the shell. Also, try to put double quotes around $file:
Code:
cat ../returnsfile >> "$file"
 
Old 06-03-2009, 01:24 PM   #7
allele
Member
 
Registered: May 2009
Posts: 42

Original Poster
Rep: Reputation: 16
Code:
$ bash -x ../sedscript 
+ sed -i '$a\\n\n' one.txt
sed: 1: "one.txt": invalid command code o
+ sed -i '$a\\n\n' two.txt
sed: 1: "two.txt": undefined label 'wo.txt'
With quotes around $file gives the same thing for sed but works for cat -- yay!

For the cat script, I must have had characters in the filenames that bash didn't like, because I just got it to work after renaming them to numbers (before I tried that part of your post).
 
Old 06-03-2009, 01:57 PM   #8
allele
Member
 
Registered: May 2009
Posts: 42

Original Poster
Rep: Reputation: 16
... I have now just realised that cat prepending doesn't work with more than one file.

Thanks for your help with appending though.

Last edited by allele; 06-03-2009 at 02:00 PM.
 
  


Reply



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
bash programming-append single line to end of file pheasand Linux - General 4 02-28-2014 09:41 AM
bash returns ambiguous redirect with date in filename rstoutmna Programming 5 09-13-2011 07:37 PM
Append string to end of file name chellemybelle Linux - Newbie 4 11-26-2007 07:17 PM
bash: append string to end of line khairil Programming 6 02-27-2007 05:09 AM
ambiguous redirect? cuss Linux - General 4 03-06-2003 10:58 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:08 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
Open Source Consulting | Domain Registration