Ambiguous redirect error and syntax error when using on multiple files


 
Thread Tools Search this Thread
Operating Systems Linux Ambiguous redirect error and syntax error when using on multiple files
# 1  
Old 08-19-2012
Ambiguous redirect error and syntax error when using on multiple files

Hi,

I need help on following linux bash script. When I linux commands for loop or while loop on individual file it runs great. but now I want the script to run on N number of files so it gives me ambiguous redirect error on line 12 and syntax error on line 22 : (pls help );

Code:
#!/bin/bash
# MCE Repricing ASPRODUCTSUMMARY

cd /u02/sppdw/dw/infa_shared/SrcFiles/POPS;
for files in $(ls *.MCESOLUTION); do  echo $files $(mv -v $files 'UNPROCESSED/ASPRODUCTSUMMARY/'${files%}.CSV) ;     done ;
cd /u02/sppdw/dw/infa_shared/SrcFiles/POPS/UNPROCESSED/ASPRODUCTSUMMARY;
####ls -1 /u02/sppdw/dw/infa_shared/SrcFiles/POPS/UNPROCESSED/ASPRODUCTSUMMARY > ListofMCECSVFilesASPRODUCTSUMMARY.txt;
FOLDERDIR="/u02/sppdw/dw/infa_shared/SrcFiles/POPS/UNPROCESSED/ASPRODUCTSUMMARY";
cd $FOLDERDIR;
for i in "ls -l | grep ^- | awk '{print $9}'"
do  
grep -i "^..............ASPRODUCTSUMMARY"< "$i" > "$i"; done;

for file in '$(ls -l $FOLDERDIR)'; do 
caseid=$(echo $file | cut -c1-10);
studytype=$(echo $file | cut -c12-15);
prefix="$caseid,$studytype,";
while read -r line
do
echo "${prefix}$line" done; < "$file" > temp.CSV;
mv -v temp.CSV ${file};
done;
exit 0;


Last edited by Scott; 08-19-2012 at 04:33 AM.. Reason: Code tags
# 2  
Old 08-19-2012
It would be helpful to post the error messages, maybe embedded in the script's output when run with options -v and/or -x set. For now, I see the following noteworthy:
Code:
for i in "ls -l | grep ^- | awk '{print $9}'"

won't fly. For command substitution use backticks or the $(...) construct instead of double quotes. BTW, there may be better ways than this to eliminate special files.
The
Code:
grep -i "^..............ASPRODUCTSUMMARY"< "$i" > "$i";

will destroy every file in your directory that does not have the grep pattern in it and reduce those who have to the lines containing the pattern. If filenames contain special chars, e.g. space, the ambiguous redirect error may result (not sure though).
The second error you mention will probably be syntax error: unexpected end of file
and is the result of a missing ; in line 19 just in front of the done
# 3  
Old 08-19-2012
Quote:
Originally Posted by RudiC
It would be helpful to post the error messages, maybe embedded in the script's output when run with options -v and/or -x set. For now, I see the following noteworthy:
Code:
for i in "ls -l | grep ^- | awk '{print $9}'"

won't fly. For command substitution use backticks or the $(...) construct instead of double quotes. BTW, there may be better ways than this to eliminate special files.
After converting from "..." to $(...) or `...`, the unquoted ^- will also be a problem in many shells. At least in the Bourne shell and Korn shell ^ is a synonym for |.
Quote:
Originally Posted by RudiC
The
Code:
grep -i "^..............ASPRODUCTSUMMARY"< "$i" > "$i";

will destroy every file in your directory that does not have the grep pattern in it and reduce those who have to the lines containing the pattern. If filenames contain special chars, e.g. space, the ambiguous redirect error may result (not sure though).
Since the all redirections occur in the shell before grepis called, that command will always either result in an empty file or a redirection error.
Quote:
Originally Posted by RudiC
The second error you mention will probably be syntax error: unexpected end of file
and is the result of a missing ; in line 19 just in front of the done
# 4  
Old 08-19-2012
syntax Error on Multiple files

First of all thank you so much for helping me RudiC and Don Cragon,

I changed the " " to $(), and quoted '^-':
Before :
Code:
for i in "ls -l | grep ^- | awk '{print $9}'"

Now : for i in
Code:
 "ls -l | grep '^-' | awk '{print $9}'"

and put the ; before done on line 19th, I am a beginner for linux so don't know much about it but this script will sol the purpose I need to do with N number of files.
I exactly want to delete unwanted lines not having word "ASPRODUCTSUMARY" from a specific position 15th from any CSV file coming in as a result of
Code:
for i in "ls -l | grep '^-' | awk '{print $9}'"

Now I am getting error on line 12 after the file name as correctly being passed. The output of the script is as follows :
Code:
[madhu@devinfaav POPS]$ bash MCEASPRODUCTSUMMARY.sh
: command not found.sh: line 3: 
: command not found.sh: line 4: 
1400009823_RTBM_20120614_1715_0_0_0_002.MCESOLUTION `1400009823_RTBM_20120614_1715_0_0_0_002.MCESOLUTION' -> `UNPROCESSED/ASPRODUCTSUMMARY/1400009823_RTBM_20120614_1715_0_0_0_002.MCESOLUTION.CSV'
1400009823_RTBM_20120614_1715_0_0_0_00.MCESOLUTION `1400009823_RTBM_20120614_1715_0_0_0_00.MCESOLUTION' -> `UNPROCESSED/ASPRODUCTSUMMARY/1400009823_RTBM_20120614_1715_0_0_0_00.MCESOLUTION.CSV'
: command not found.sh: line 5: 
: command not found.sh: line 6: 
: command not found.sh: line 8: 
: command not found.sh: line 9: 
: command not found.sh: line 11: 
: command not found.sh: line 11: 
: command not found.sh: line 11: 
: command not found.sh: line 11: 
: No such file or directorye 14: 1400009823_RTBM_20120614_1715_0_0_0_00.MCESOLUTION.CSV
: command not found.sh: line 12: 
: command not found.sh: line 13: 
MCEASPRODUCTSUMMARY.sh: line 26: syntax error near unexpected token `done'
'CEASPRODUCTSUMMARY.sh: line 26: `echo "${prefix}$line"; done< "$file" > temp.CSV;


UPDATED SCRIPT:
Code:
#!/bin/bash
# MCE Repricing ASPRODUCTSUMMARY

# Moves file in desired folder and changes them the CSV file types
cd /u02/sppdw/dw/infa_shared/SrcFiles/POPS;
for files in $(ls *.MCESOLUTION); do  echo $files $(mv -v $files 'UNPROCESSED/ASPRODUCTSUMMARY/'${files%}.CSV) ;     done ;
cd /u02/sppdw/dw/infa_shared/SrcFiles/POPS/UNPROCESSED/ASPRODUCTSUMMARY;

# below will delete unnecessary lines from the file in current directory
FOLDERDIR="/u02/sppdw/dw/infa_shared/SrcFiles/POPS/UNPROCESSED/ASPRODUCTSUMMARY";
cd $FOLDERDIR;
for i in $(ls -l | grep '^-' | awk '{print $9}')
do  
grep -i "^.............ASPRODUCTSUMMARY"< "$i" > "$i"; done;
# to add case id and studytype value in front of each line of each file(N files)
# for example caseid='1400009823' and studytype ='RTBM' which are 
# extracted from filename and then appended in front of each line of a line
# and then repeat for N number of CSV files in current directory

for file in '$(ls -l $FOLDERDIR)'; do 
caseid=$(echo $file | cut -c1-10);
studytype=$(echo $file | cut -c12-15);
prefix="$caseid,$studytype,";
while read -r line
do
echo "${prefix}$line"; done< "$file" > temp.CSV;
mv -v temp.CSV ${file};
done;
exit 0;

Thank you so much in advance
Best Regards

Last edited by Neo; 05-06-2019 at 01:55 PM..
# 5  
Old 08-19-2012
As far as I can see, the script is intended to analyse the names of files found in a directory and produce a list of those files (prefixed by two comma-separated fields derived from the filename) in a file called temp.CSV in the same directory. This however does not tie up with the description in your previous post which implies some data edit.

Before you end up trashing every file in your directory with "trial-and-error" scripting, please post sample filenames and a full description of the process complete with matching sample output.

Also, please use code tags for code and data samples.

It is never necessary or desirable to end a line of a Bourne Shell script with a semi-colon (except for the one exception of certain syntax of the "find" command). This is unix script language not Oracle or Perl.

Last edited by methyl; 08-19-2012 at 03:45 PM..
# 6  
Old 08-19-2012
Please use the CODE and /CODE tags around your scripts. (Highlight the code and then press the symbol in the Message: menu that has the work CODE in a rectangular box with a hand pointing into the box.) When you don't, HTML processing can significantly change the meaning of what appears to be your code. Please either edit you last posting to include these tags, or repost your code (with tags) in a new message.

Note, however, that you still have the command line:
Code:
grep -i "^.............ASPRODUCTSUMMARY"< "$i" > "$i"; done;

which, as we mentioned before, in this loop will delete everything in every regular file in the current directory.

I also note that the command:
Code:
 echo $files $(mv -v $files 'UNPROCESSED/ASPRODUCTSUMMARY/'${files%}.CSV)

seems strange. The mv -vwrites the source and target files to stdout, and when combined with the echo $files in front of that, you're writing the source file twice on these output lines. Then the ${files%} is missing the word that identifies the pattern that is to be removed from the end of the expansion of the files variable.
# 7  
Old 08-19-2012
syntax Error on Multiple files

Hi Don, Yes, that true I need to delete all lines from the file in current directory not having word "ASPRODUCTSUMMARY" on 15th position.
Also the thing you mentioned of the code line :
Code:
echo $files $(mv -v $files 'UNPROCESSED/ASPRODUCTSUMMARY/'${files%}.CSV)

is missing the pattern to be removed, I corrected it to be
Code:
echo $files $(mv -v $files 'UNPROCESSED/ASPRODUCTSUMMARY/'${files%.MCESOLUTION}.CSV)

main problem is for loop syntax having while loop, which is giving syntax error:
Code:
MCEASPRODUCTSUMMARY.sh: line 26: syntax error near unexpected token `done'
'CEASPRODUCTSUMMARY.sh: line 26: `echo "${prefix}$line"; done< "$file" > temp.CSV;

Now I am not getting ambiguous redirect, but snytax error.

Last edited by Madhusudan Das; 08-19-2012 at 05:29 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

$1”: ambiguous redirect

New to the site, please let me know I'm not meeting the post guidelines. I'm creating a bash script to generate a report with output from a grep command. The goal is to direct the output to a different log file by using a 'logger file'. But I get this error during the run: $1: ambiguous... (5 Replies)
Discussion started by: dallas88
5 Replies

2. Shell Programming and Scripting

Ambiguous output redirect in xterm

Hi all, I've been working on a bash script to help with backups that I have to do at work. One of the lines in the script is supposed to launch an xterm, log into a specific server node and launch a tar backup to tape. This part works ok, but I've been trying to get stdout and stderr to... (2 Replies)
Discussion started by: Exitalterego
2 Replies

3. Shell Programming and Scripting

Ambiguous error

Hello everybody, I just took over this job from someone else and in the past this script they built worked but i recently upgraded from openSuSe 11.4 to 12.1 Now when i run the script i get an ambiguous error at line 25 (the first line after add() ) I have edited out the webpath and any... (1 Reply)
Discussion started by: gumbicus
1 Replies

4. Shell Programming and Scripting

Receiving 'ambiguous redirect' when trying to run command against multiple files

I came across the command string on https://www.unix.com/shell-programming-scripting/141885-awk-removing-data-before-after-pattern.html which was what I was looking for to be able to remove data before a certain pattern. However, outputting the result to a file seems to work on an individual basis... (4 Replies)
Discussion started by: HLee1981
4 Replies

5. Shell Programming and Scripting

ambiguous redirect error

This script has ambiguous redirect error. ... cd $HOME cd folder/work # search all subfolders in work directory find -mindepth 1 -maxdepth 1 -type d | while read directory do CUR_FOLDER="${directory#"./"}" cd $CUR_FOLDER chmod 644 * for ff in *; do if ; then ... (5 Replies)
Discussion started by: candyme
5 Replies

6. Shell Programming and Scripting

Ambiguous redirect

Hello there, I'm totally new in bash programming and ran into my first problem. My script should generate 3 textfiles where the content of the first and the third row are the same in each file. Only the second row is different. This is what I did in a very simplified explanation: ... (6 Replies)
Discussion started by: johndoe
6 Replies

7. UNIX for Dummies Questions & Answers

ambiguous redirect issue

I am trying to run the following script and I am getting an "ambiguous redirect" error. I have checked to make sure that the files are all where I have specified and are read/write as needed. Any ideas? Note: I have removed the actual path info for privacy sake. I have triple checked to make... (1 Reply)
Discussion started by: malantha
1 Replies

8. Shell Programming and Scripting

> to empty files, but ambiguous redirect

Hi Everyone, # ll total 0 -rw-r--r-- 1 root root 0 2010-05-13 11:29 a1.log -rw-r--r-- 1 root root 0 2010-05-13 11:29 a2.log -rw-r--r-- 1 root root 0 2010-05-13 11:29 a3.log # rm a.log above rm no problem, but when i use "> a.log", it says "-bash: a.log: ambiguous redirect". ... (3 Replies)
Discussion started by: jimmy_y
3 Replies

9. Shell Programming and Scripting

Ambiguous output redirect error

Hi everyone, While I was trying to do DATE=`date +"%Y%m%d_%H%M%S"` STARTLOG=$TUXSTDDIR/start_$DATE.log tmboot -y > $STARTLOG 2>&1 I got an error i.e. Ambiguous output redirect error. Here the first part is to boot the account so there is nothing wrong with that.... (6 Replies)
Discussion started by: pareshan
6 Replies

10. UNIX for Dummies Questions & Answers

ambiguous redirect

i have following statement in the script echo -e "$str_XML_col_name:$str_field_type;" >> $i_DC_Key_$i_Tgt_DC_key_Schema here $i_DC_Key is DC key and $i_Tgt_DC_key are the variables............... when i ran the script i am getting error rec_merge.sh: $i_DC_Key_$i_Tgt_DC_key_Schema:... (1 Reply)
Discussion started by: mahabunta
1 Replies
Login or Register to Ask a Question

Featured Tech Videos