for cat help


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers for cat help
# 1  
Old 08-14-2009
for cat help

Hi there,

I have the following problem. I have a csv file which looks like

Code:
'AGI,ABJ,Y,Y,Y,None,EQUATION,ANY,ANY,None,'
'AGI,ABJ,Y,Y,Y,None,EQUATION HEAVY,ANY,ANY,None,'
'AGI,ABJ,Y,Y,Y,None,VARIATION,ANY,ANY,None,'

but I do this
Code:
for ab in $(cat test.csv); do echo $ab; done

in the output it replaces the spaces with CR

Code:
'AGI,ABJ,Y,Y,Y,None,EQUATION,ANY,ANY,None,'
'AGI,ABJ,Y,Y,Y,None,EQUATION
HEAVY,ANY,ANY,None,'
'AGI,ABJ,Y,Y,Y,None,VARIATION,ANY,ANY,None,'

why is this happening?

I want to do at the end this
Code:
for ab in $(cat test.csv); do echo $ab; grep $ab file.csv; done

But the above problem results that grep output is wrong.

Any ideas?

Thanks!
# 2  
Old 08-14-2009
Hi.

If you want to process each line as a whole, then try using a while loop instead:

Code:
while read LINE; do
  echo $LINE
  grep "$LINE" file.csv
done < test.csv

It's not inserting a newline, the for is taking each white-space-separated field one at a time.
# 3  
Old 08-14-2009
or this may work..
Code:
grep -f test.csv file.csv

# 4  
Old 08-14-2009
Quote:
Originally Posted by vidyadhar85
or this may work..
Code:
grep -f test.csv file.csv

No this doesn't work, I guess because it takes all the lines together as one pattern

---------- Post updated at 11:19 AM ---------- Previous update was at 11:15 AM ----------

Quote:
Originally Posted by scottn
Hi.

If you want to process each line as a whole, then try using a while loop instead:

Code:
while read LINE; do
  echo $LINE
  grep "$LINE" file.csv
done < test.csv

It's not inserting a newline, the for is taking each white-space-separated field one at a time.
This works great! Thanks
# 5  
Old 08-14-2009
Great! But for what you're trying to do, the grep option should also work. Try it with "grep -Ff" if just -f doesn't work:

Code:
> cat file1
'AGI,ABJ,Y,Y,Y,None,EQUATION,ANY,ANY,None,'
'AGI,ABJ,Y,Y,Y,None,EQUATION HEAVY,ANY,ANY,None,'
'AGI,ABJ,Y,Y,Y,None,VARIATION,ANY,ANY,None,'

> cat file2
'GI,ABJ,Y,Y,Y,None,EQUATION,ANY,ANY,None,'
'AGI,ABJ,Y,Y,Y,None,EQUATION HEAVY,ANY,ANY,None,'
'AGI,ABJ,Y,Y,Y,None,VARIATION,ANY,ANY,None,'

> grep -f file2 file1
'AGI,ABJ,Y,Y,Y,None,EQUATION HEAVY,ANY,ANY,None,'
'AGI,ABJ,Y,Y,Y,None,VARIATION,ANY,ANY,None,

 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Cat with << >>

Hi, When I was analyzing the code I got below line. cat - << 'EOF' >> ${FILE PATH} I surfed net to understand but I couldn't get what is about. Please help me out. (2 Replies)
Discussion started by: stew
2 Replies

2. UNIX for Advanced & Expert Users

Help need on cat command

I have two files as below 1.txt AA 123 CC 145 DD 567 2.txt AA 111 YY 128 CC 144 FF 222 DD 777 ZZ 875 basically 1.txt is updated file, if i do cat 1.txt 2.txt output should be as below o/p (4 Replies)
Discussion started by: Tecnical_help12
4 Replies

3. Shell Programming and Scripting

Cat command help

I want to concatenate 100 files to one file and append file name in each record to find out which file it came from for a in $(<shal_group) do cat $a >> bigoutput.group The above code put all files in one file but i want file name appended to each file Record should be like this... (3 Replies)
Discussion started by: pinnacle
3 Replies

4. UNIX for Dummies Questions & Answers

cat and export

I have a file named filelist. the content is a list of files including the path. $ cat filelist $curdir/test1 $curdir/test2 I want to cat each file in the list, such as cat $curdir/test1, cat $curdir/test2. (The $curdir has been exported). it can't open the test1/test2, it can't change... (3 Replies)
Discussion started by: steven_TTG
3 Replies

5. Shell Programming and Scripting

cat in the command line doesn't match cat in the script

Hello, So I sorted my file as I was supposed to: sort -n -r -k 2 -k 1 file1 | uniq > file2 and when I wrote > cat file2 in the command line, I got what I was expecting, but in the script itself ... sort -n -r -k 2 -k 1 averages | uniq > temp cat file2 It wrote a whole... (21 Replies)
Discussion started by: shira
21 Replies

6. Shell Programming and Scripting

Cat of rows

Hello, I'm starting from the scratch with Unix, and I was wondering if you could give me an answer for this problem... I've got a column with different names of files, something like: ./file1 ./file2 ... Now, I would like to show the content of each file. The column with the names comes... (5 Replies)
Discussion started by: kalius88
5 Replies

7. UNIX for Dummies Questions & Answers

'shutdown' and 'cat'

Hi All, This is actually a good interview question. On linux, the permissions and group for 'shutdown' and 'cat' is the same. -rwxr-xr-x 1 root root 18K 2008-05-21 10:43 shutdown -rwxr-xr-x 1 root root 17K 2007-01-30 19:51 cat Then why is it that a... (5 Replies)
Discussion started by: scottsiddharth
5 Replies

8. Shell Programming and Scripting

for i in `cat myname.txt` && for y in `cat yourname.txt`

cat myname.txt John Doe I John Doe II John Doe III ----------------------------------------------------------------------- for i in `cat myname.txt` do echo This is my name: $i >> thi.is.my.name.txt done ----------------------------------------------------------------------- cat... (1 Reply)
Discussion started by: danimad
1 Replies

9. UNIX for Dummies Questions & Answers

Difference between cat , cat > , cat >> and touch !!!

Hi Can anybody tell the difference between Difference between cat , cat > , cat >> and touch command in UNIX? Thanks (6 Replies)
Discussion started by: skyineyes
6 Replies
Login or Register to Ask a Question