Find every alternate word in a csv


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find every alternate word in a csv
# 1  
Old 11-21-2014
Find every alternate word in a csv

Hi,

This should be really simple. But not sure why I am unable to crack it.
I am looking for a simple one liner to print every alternate word in a csv. say my file is having content :

Code:
abc,def,ghi,jkl,.....

i need the output as below

Code:
abc
ghi

# 2  
Old 11-21-2014
Any attempts from your side?
# 3  
Old 11-21-2014
trial

Code:
#!/bin/bash

while IFS=',' read -r f1 f2 f3 f4 f5 f6 f7
do
  echo "$f1
 $f3
 $f5
 $f7"
done < "sample.txt"

But its a huge csv and this is not the right solution
# 4  
Old 11-21-2014
Try
Code:
awk -F, '{ for (i=1;i<=NF;i+=2) print $i }' file

This User Gave Thanks to junior-helper For This Post:
# 5  
Old 11-21-2014
Alternatively with mawk or gawk :
Code:
awk 'NR%2' RS='[,\n]'

Note that this will produce a different result, depending on the number of fields per line and the number of lines, which may or may not be what you are looking for with "every alternate word" (is that per line or per file?) (For a single line or if there is always an even number of fields per line, there will be no difference).

for example:
Code:
$ echo "1,2,3,4,5
a,b,c,d,e" | mawk NR%2 RS='[,\n]'
1
3
5
b
d

Junior Helper's solution:
Code:
$ echo "1,2,3,4,5
a,b,c,d,e" | awk -F, '{ for (i=1;i<=NF;i+=2) print $i }'
1
3
5
a
c
e

Probably you are looking for Junior Helper's suggestion but this may be of interest for somebody else looking at this thread..

Last edited by Scrutinizer; 11-23-2014 at 12:42 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 11-21-2014
This may not be very elegant but with sed and awk

Code:
echo "abc,def,ghi,jkl" | sed 'y/,/\n/;' | awk 'NR%2==1'

sed (GNU sed) 4.2.2
GNU Awk 4.1.1
This User Gave Thanks to ni2 For This Post:
# 7  
Old 11-21-2014
Thanks so much for taking the time to help me.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find a word and increment the number in the word & save into new files

Hi All, I am looking for a perl/awk/sed command to auto-increment the numbers line in file, P1.tcl: run_build_model sparc_ifu_dec run_drc set_faults -model path_delay -atpg_effectiveness -fault_coverage add_delay_paths P1 set_atpg -abort_limit 1000 run_atpg -ndetects 1000 I would like... (6 Replies)
Discussion started by: jypark22
6 Replies

2. Shell Programming and Scripting

Find word in a line and output in which line the word occurs / no. of times it occurred

I have a file: file.txt, which contains the following data in it. This is a file, my name is Karl, what is this process, karl is karl junior, file is a test file, file's name is file.txt My name is not Karl, my name is Karl Joey What is your name? Do you know your name and... (3 Replies)
Discussion started by: anuragpgtgerman
3 Replies

3. Shell Programming and Scripting

Shell Script @ Find a key word and If the key word matches then replace next 7 lines only

Hi All, I have a XML file which is looks like as below. <<please see the attachment >> <?xml version="1.0" encoding="UTF-8"?> <esites> <esite> <name>XXX.com</name> <storeId>10001</storeId> <module> ... (4 Replies)
Discussion started by: Rajeev_hbk
4 Replies

4. UNIX for Dummies Questions & Answers

Word Wrap .CSV Fils

Is there a generic method for applying word wrap to all cells for a .csv file? (6 Replies)
Discussion started by: jimmyf
6 Replies

5. Shell Programming and Scripting

perl lwp find word and print next word :)

hi all, I'm new there, I'm just playing with perl and lwp and I just successfully created a script for log in to a web site with post. I have a response but I would like to have something like this: I have in my response lines like: <div class="sender">mimi020</div> <some html code.....>... (3 Replies)
Discussion started by: vogueestylee
3 Replies

6. UNIX for Dummies Questions & Answers

Find EXACT word in files, just the word: no prefix, no suffix, no 'similar', just the word

I have a file that has the words I want to find in other files (but lets say I just want to find my words in a single file). Those words are IDs, so if my word is ZZZ4, outputs like aaZZZ4, ZZZ4bb, aaZZZ4bb, ZZ4, ZZZ, ZyZ4, ZZZ4.8 (or anything like that) WON'T BE USEFUL. I need the whole word... (6 Replies)
Discussion started by: chicchan
6 Replies

7. Shell Programming and Scripting

Find and replace a word in all the files (that contain the word) under a directory

Hi Everyone, I am looking for a simple way for replacing all the files under a directory that use the server "xsgd1234dap" with "xsdr3423pap". For Example: In the Directory, $pwd /home/nick $ grep -l "xsgd1234dap" *.sh | wc -l 119 I have "119" files that are still using... (5 Replies)
Discussion started by: filter
5 Replies

8. Shell Programming and Scripting

How to replace word in CSV

Hi, Can someone please help? I am using Sun OS unix: 1. read a CSV file, port.csv 2. find out all lines which has word "Documentation"..as I only need to change on those lines which has word, "Documentation" 3. then for each found line, I have to read the 9th element, which always has the... (4 Replies)
Discussion started by: kinmak
4 Replies

9. Shell Programming and Scripting

find a word in a file, and change a word beneath it ??

Hi all, I have a file with lines written somewhat like this. aaaa ccc aa linux browse = no xssxw cdcedc dcsdcd csdw police dwed dwd browse = no cdecec (2 Replies)
Discussion started by: vikas027
2 Replies

10. Shell Programming and Scripting

Write a shell program to find the sum of alternate digits in a given 5-digit number

Hi Can any one please post the answer for the above program.................. (4 Replies)
Discussion started by: banta
4 Replies
Login or Register to Ask a Question