Concatenate strings in a a for loop


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Concatenate strings in a a for loop
# 1  
Old 09-11-2014
Concatenate strings in a for loop

hi guys, I have this question. I am creating an script to that read a text file(.ini) with the list of the patterns to find for example:
Code:
EPMS_VO
EMPS_PARTS

Then it check if file have been delivered in a folder and process it with each pattern, but I am having problems concatenting the variables (FILE+LINE+*.csv) .
For the first entry in the ini file "EMPS_VO" it should read it and form this:
TEST=/MYPATH/EMS_VO*.csv and this tested it in FILESEXIST

Any ideas to accomplish this?

Code
====================
Code:
FILEDIR="MYPATH"
for FILE in `ls *feeds.ini`
do
  echo $FILE
  for LINE in `cat $FILE `
  TEST=$FILEDIR$LINE/*.csv
  do
    FILESEXIST=`ls -1 $TEST 2>/dev/null | wc -l`
 
    if [[ $FILESEXIST -eq 0 ]]
    then
      echo "***** No Vendors files found to process *****"
    else
      echo "files were located"
    fi
  done
done


Last edited by Scrutinizer; 09-11-2014 at 11:32 PM.. Reason: Add CODE and ICODE tags. + Added indenting for legibility
# 2  
Old 09-11-2014
What problem are you experiencing? Can you give an example of "MYPATH" that is assigned to the FILEDIR variable? I notice that there is a concatenation of $FILEDIR (rather than $FILE) , $LINE and *.csv. What results do you get and what do you expect?
# 3  
Old 09-12-2014
hi what I am expecting is to list all the files in an specific an specific folder and patter like this
Code:
/MYPATH/EPMS_VO*.csv

/MYPATH/ will be fixed but the ini file could have more than one pattern in this EPMS_VO (there is a typo on the original post).

The problem is when I concatenate fixed dir FILEDIR with LINE it
Code:
"TEST=$FILEDIR$LINE/*.csv"

intead of get this :
Code:
/MYPATH/EPMS_VO*.csv

I am getting this and failig:
Code:
*.csvPATH/EMPS_VO

So the condition FILESEXIST is always failing
# 4  
Old 09-12-2014
Probably there are DOS style CR characters in your input file (DOS format). Try using
Code:
tr -d '\r' < file > newfile

to remove them and to turn the files into UNIX format.

Last edited by Scrutinizer; 09-12-2014 at 02:25 AM..
# 5  
Old 09-12-2014
And, with
Code:
TEST=$FILEDIR$LINE/*.csv
                  ^--- that

you will never get
Code:
/MYPATH/EPMS_VO*.csv
               ^--- this

# 6  
Old 09-12-2014
Welcome Danman,

You can also neaten your code. For the loop:-
Code:
for FILE in `ls *feeds.ini`
do ........

.... you can use
Code:
for FILE in *feeds.ini
do ........

.
To test if a file exists, convert from:-
Code:
FILESEXIST=`ls -1 $TEST 2>/dev/null | wc -l`

if [[ $FILESEXIST -eq 0 ]]
then ........

.... to just:-
Code:
if [ -f $TEST ]
then ........

If you need to set this with a wild-card in the test, you can achieve that by using:-
Code:
TEST="${FILEDIR}${LINE}/\*.csv"

I've added curly brackets (I think braces is the proper name) to be very clear on the variable names too.


I hope that these help,
Robin
# 7  
Old 09-12-2014
Quote:
Originally Posted by rbatte1
If you need to set this with a wild-card in the test, you can achieve that by using:-
Code:
TEST="${FILEDIR}${LINE}/\*.csv"

I've added curly brackets (I think braces is the proper name) to be very clear on the variable names too.
You do not even need to use escapes if you enclose your variables in double quotes:

Code:
file="foo*bar"

if [ -f "$file" ] ; then
     echo "file ${file} exists"
else
     echo "file ${file} does not exist"
fi

try this out with a file literally named "foo*bar". To find files fitting the file glob (like i.e., "foo1.bar", etc.) remove the double quotes in the test only:

Code:
file="foo*bar"

if [ -f $file ] ; then                    # <-- notice the difference
     echo "file ${file} exists"
else
     echo "file ${file} does not exist"
fi

See also this thread in the Emergency forum

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Concatenate strings

Hi,I'm trying to concatenate @example.com to each line of a file f1.txt. and push it into f2.txt. Here is the code i'm using. for i in `cat /home/linux1/xxxxxxx/f1.txt`; do echo ${i}@example.com > /home/linux1/xxxxxx/f2.txt; done But above code only printing @example.com in f2.txt. what... (18 Replies)
Discussion started by: sam_bd
18 Replies

2. Programming

Concatenate two strings

Hello! Can anyone explain line 28 for me? I was thinking *a would be replaced by *b, but it actually appends *a to *b. I know it is related to pointer address, but could not figure it out by myself. Thanks a lot! 1 //Concatenate two strings 2 3 #include<stdio.h> 4 char *stradd (char *,... (5 Replies)
Discussion started by: yifangt
5 Replies

3. Shell Programming and Scripting

Concatenate text between patterns in individual strings

In any given file, wherever a certain data block exists I need to concatenate the values(text after each "=" sign) from that block. in that block. The block starts and ends with specific pattern, say BEGIN DS and END DS respectively. The block size may vary. A file will have multiple such blocks.... (12 Replies)
Discussion started by: Prev
12 Replies

4. Web Development

Concatenate Strings

hi..:) this is my sample part of my program.. $csv_output .= $row.",". $row.",". $row.",". $row.",". $row.",". ... (2 Replies)
Discussion started by: Jeneca
2 Replies

5. UNIX for Dummies Questions & Answers

concatenate strings

if i use echo "ravi" echo "sankar" it showing output ravi sankar but i want output as ravi sankar remember sankar should be in another echo statement only (2 Replies)
Discussion started by: shankr3
2 Replies

6. Shell Programming and Scripting

delete repeated strings (tags) in a line and concatenate corresponding words

Hello friends! Each line of my input file has this format: word<TAB>tag1<blankspace>lemma<TAB>tag2<blankspace>lemma ... <TAB>tag3<blankspace>lemma Of this file I need to eliminate all the repeated tags (of the same word) in a line, as in the example here below, but conserving both (all) the... (2 Replies)
Discussion started by: mjomba
2 Replies

7. UNIX for Dummies Questions & Answers

Concatenate Strings

Hi Friends, I have a requirement I need to concatenate the below two strings. String 1 = /@jobid_at_ String 2 = value stored in ORACLE_SID String 3 = string1 concatenated with String 2. Please let me know how should i do it in UNIX. Thanks, (2 Replies)
Discussion started by: diva_thilak
2 Replies

8. Shell Programming and Scripting

How to concatenate two strings or several strings into one string in B-shell?

like connect "summer" and "winter" to "summerwinter"? Can anybody help me? thanks a lot. (2 Replies)
Discussion started by: fontana
2 Replies
Login or Register to Ask a Question