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
# 15  
Old 08-20-2012
syntax Error on Multiple files

Hi Don, Thanks for explaining how linux OS will behave on my previously provided coding. That's why it was giving so many errors.

Can you help me with the last peace, now I was able to run upto
[CODE]for i in $(ls -l | grep '^-' | awk '{print $9}')
do
grep -i "^..............ASPRODUCTSUMMARY" < "$i" > "newtemp.csv" mv -v newtemp.csv "$i"
done/[CODE]
I ran the following code you reformatted:

Code:
#!/bin/bash
for i in $(ls -l | grep '^-' | awk '{print $9}')
do 
        grep -i "^..............ASPRODUCTSUMMARY" < "$i" > "newtemp.csv"        mv -v newtemp.csv "$i"
done
for file in $(ls)
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 "$i"
done
exit 0

The problem is at line 8
Code:
for file in $(ls)
do

. The code you provided above is giving syntax error on this line 8.

What I did is, I have separated the tasks, that is I made a separate script to copy files. I made separate script to delete unwanted lines from the copied files. and the finally separate script to append
Quote:
CASEID
and
Quote:
STUDYTYPE
in each line of each file. but this last script is not working when run on multiple files. Please help. How will you code such requirement.
# 16  
Old 08-20-2012
Quote:
Originally Posted by Madhusudan Das
Hi Don, Thanks for explaining how linux OS will behave on my previously provided coding. That's why it was giving so many errors.

Can you help me with the last peace, now I was able to run upto
Code:
for i in $(ls -l | grep '^-' | awk '{print $9}')
do 
        grep -i "^..............ASPRODUCTSUMMARY" < "$i" > "newtemp.csv"        mv -v newtemp.csv "$i"
done

*** I changed the last line above to correctly terminate a code tag. And, I had a cut and paste error that combined two lines that I had in the file I copied from a file on my machine. The grep and mv commands should have been on separate lines. dwc ***

I ran the following code you reformatted:

Code:
#!/bin/bash
for i in $(ls -l | grep '^-' | awk '{print $9}')
do 
        grep -i "^..............ASPRODUCTSUMMARY" < "$i" > "newtemp.csv"        mv -v newtemp.csv "$i"
done
for file in $(ls)
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 "$i"
done
exit 0

The problem is at line 8
Code:
for file in $(ls)
do

. The code you provided above is giving syntax error on this line 8.

What I did is, I have separated the tasks, that is I made a separate script to copy files. I made separate script to delete unwanted lines from the copied files. and the finally separate script to append and in each line of each file. but this last script is not working when run on multiple files. Please help. How will you code such requirement.
The code that I reformatted was only the last part of your script. I thought I had talked you through the changes that needed to be made to the first part.
As I mentioned in-line in the quote from your message, I made a cut and paste error. One line in the reformatted code I posted should have been:
Code:
        grep -i "^..............ASPRODUCTSUMMARY" < "$i" > "newtemp.csv"
        mv -v newtemp.csv "$i"

but was a single line in what I pasted. I imagine that this is what is causing many of your problems. I sincerely apologize for causing this confusion.

I have no idea what code you are now using. After splitting the code into three scripts I have no idea whether the variables you are using in the second and third scripts were set in the first or second scripts and are, therefore, unset or incorrectly set in the later scripts. Please post each script that you now have as separate code segments (with the CODE and /CODE tags) and put the names of each script in the description of what each code segment is. Also, please include an example of a line of output from the
Code:
grep -i "^..............ASPRODUCTSUMMARY" < "$i" > "newtemp.csv"

so we have some idea of what might appear in the values of $prefix.
I have meeting later this morning and am going to have to actually go to bed soon. If you can give me a complete, clear listing of the code you're now using and describe the differences between what you're getting and what you want I may be able to go through this again this evening. (It is now after 1:30 AM PDT where I'm located.) Smilie
This User Gave Thanks to Don Cragun For This Post:
# 17  
Old 08-20-2012
Syntax error on multiple files script

Hi Don,
I have posted the separate script now I am using in POST#11.
1) code is coping files only :
Code:
#!/bin/bash
# copying MCE Repricing ASPRODUCTSUMMARY
cd /u02/sppdw/dw/infa_shared/SrcFiles/POPS;
for files in $(ls *.MCESOLUTION);do $(cp -v $files 'UNPROCESSED/ASPRODUCTSUMMARY/'${files%.MCESOLUTION}.CSV); done;
exit 0;

2) second script is deleting unwanted lines from the files copied in above script #1
Code:
#!/bin/bash
# MCE Repricing ASPRODUCTSUMMARY ONLY DATA
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" > newfile);
$(mv -v "newfile" "$i"); done;
exit 0;

ABOVE two script s are working perfectly fine and doing they are are supposed to.
3) I need help in the last script below, which should extract the caseid and studytype from filename itself and then append these two values(separated by ,) in front of each line of each file provided by the [list] of
Code:
for

loop. The code for this I have is:
Code:
#!/bin/bash
#!/bin/bash
# MCE Repricing ASPRODUCTSUMMARY Append Casid n studytype in each line
FOLDERDIR='/u02/sppdw/dw/infa_shared/SrcFiles/POPS/UNPROCESSED/ASPRODUCTSUMMARY';
cd $FOLDERDIR;
for f in $(ls *.CSV); do caseid="$(echo "$f" | cut -c1-10)";
studytype="$(echo "$f" | cut -c12-15)";
prefix="$caseid,$studytype,";
echo $prefix;
while read line 
do echo "${prefix}$line" done < "$f" > "newtempfile";
mv -v "newtempfile" "$f";
done;
exit 0;

Getting syntax error
Quote:
line 11:syntax error: unexpected end of file
---------- Post updated at 04:02 PM ---------- Previous update was at 09:39 AM ----------

Thank you all who tried to help me.

the following coding resolved the problem and I could achieve what I needed to:
Code:
#!/bin/bash
# MCE Repricing ASPRODUCTSUMMARY Append Casid n studytype in each line
FOLDERDIR='/u02/sppdw/dw/infa_shared/SrcFiles/POPS/UNPROCESSED/ASPRODUCTSUMMARY';
cd $FOLDERDIR;
for f in $(ls *.CSV); do caseid="$(echo "$f" | cut -c1-10)"
studytype="$(echo "$f" | cut -c12-15),"
prefix="${caseid//[[:space:]]/},${studytype//[[:space:]]/}";
while read line; do echo -n "${prefix}$line" ;done< "$f" > newtempfile;
$(mv -v "newtempfile" "$f");
done;
exit 0;

putting
Code:
//[[:space:]]/

was also useful to eliminate line characters otherwise it was adding a new line each time.
Going to close this post as resolved.

Last edited by Madhusudan Das; 08-20-2012 at 02:15 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