0

i'm learning bash scripting and i wrote this simple script:

#!/bin/bash
for f in $(cat test.csv | cut -d';' -f 4)
do
EXT=".pdf"
echo "$f$EXT"
done

Where the file test.csv is:

col1;col2;col3;test1
col1;col2;col3;test2
col1;col2;col3;test3
...

the output of this script is:

.pdf
.pdf
.pdf
...

The output expected would be:

test1.pdf
test2.pdf
test3.pdf
...

Sorry for stupid questions but i'm stuck in this, i think, little problem.

Same output with this script:

#!/bin/bash
while IFS=\; read col1 col2 col3 col4
do
echo "$col4".pdf
done < test.csv
6
  • 1
    Is it your real test.csv? You're extracting the 4th column among columns separated by semicolon, but your example doesn't have 4th column. Commented Jan 5, 2013 at 19:20
  • Yeah, the content of test.csv interested in is the 4th column. Fixed. Commented Jan 5, 2013 at 19:22
  • 1
    If test.csv indeed has 4 columns or more, edit your question and show the real example (where we can see 4 columns even if they're uninteresting). Commented Jan 5, 2013 at 19:24
  • Just tried your original for loop and it works fine for me. Is there anything else in the script that you've cut out perhaps? Alternatively, are you sure there aren't control characters in your input file test.csv? BTW How are you running the script? Commented Jan 5, 2013 at 19:58
  • Importato to note that the script work with oncly echo "$col4" without .pdf string. I run it via macosx terminal ./script Commented Jan 5, 2013 at 20:11

3 Answers 3

1

Do not do for i in $(cat something). It will keep the whole file in memory which isn't needed. Also it doesn't work with whitespace in columns.

Better parse line by line, and use shell tools to split the columns (the read builtin).

Something along the lines of

while IFS=\; read col1 col2 col3 col4
do
    echo "$col4".pdf
done < test.csv

should do.

You can remove the IFS=\; part if your columns are separated by whitespace instead.

Sign up to request clarification or add additional context in comments.

4 Comments

To simplify i created a file test with the output of cat test.csv | cut -d';' -f 4 >> test and modified the script: while read f do echo "$f".pdf done < test and actually the output it's the same of my script. A long list of ".pdf"...
Not working yet...always a list of .pdf .pdf .pdf. ... it's something like he loose the value of $col4
@Kartone: Yes it works. If it doesn't for you, then you probably use a different test set than the one you provided to us.
i'll try to change test file.
1

Or use sed:

sed  's/.*;\(.*\)/\1.pdf/' file

Comments

0

Try this:

#!/bin/bash
while read line
do
        LINE=`echo "$line" | awk -F';' '{print $4}'`
        echo "${LINE}.pdf"
done < test.csv

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.