Grep Delimited Line and Display Vertically


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep Delimited Line and Display Vertically
# 1  
Old 05-09-2013
Grep Delimited Line and Display Vertically

Hello All,

I am trying to take a colon-delimited line from a bunch of lines such as

Code:
apple:green:5cents:CA
apple:red:4cents:FL
orange:green:6cents:HI

...and display it vertically with label prefixes such as the following;

Code:
Fruit: apple
Color: green
Price: 5cents
Origin: CA

So far, I have the following;

Code:
myfunc() {
     grep -i $fruitname fruitsfile | cut -f0-

if [ $1 == 0 ]
then
     echo "Fruit: -f0\nColor: -f1\nPrice: -f2\nOrigin: -f3"
else
     echo "$1 does not exist."
fi
}

myfunc $fruitname

...and it just lists the lines several times instead. Any idea?

Moderator's Comments:
Mod Comment Please use code tags also for data


---------- Post updated at 07:30 PM ---------- Previous update was at 03:59 PM ----------

Sorry about that, will wrap up data as well going forward.

Okay, so I was able to get what I wanted with the following code:

Code:
myfunc() {
     check=$(grep -ci $fruitname fruitsfile)

if [ "$check" != 0 ]
then
     grep -iw $fruitname fruitsfile | awk 'BEGIN { FS="\n"; RS=":";} {print $0}'
else
     echo "$1 does not exist."
fi
}

myfunc $fruitname

...to get the following:

Code:
apple
green
5cents
CA

However, now I need to add the prefixes before each line so the output looks like:

Code:
Fruit: apple
Color: green
Price: 5cents
Origin: CA

Any help will be appreciated.

Last edited by techieg; 05-09-2013 at 05:47 PM.. Reason: additional code tags
# 2  
Old 05-09-2013
hope this is close enough to what you need:

Code:
echo 'apple:green:5cents:CA
apple:red:4cents:FL
orange:green:6cents:HI' | awk '
        BEGIN { FS=":" }
        {
                print "Fruit: " $1
                print "Color: " $2
                print "Price: " $3
        }
'

results
Code:
Fruit: apple
Color: green
Price: 5cents
Fruit: apple
Color: red
Price: 4cents
Fruit: orange
Color: green
Price: 6cents

# 3  
Old 05-09-2013
In your example, below works just fine:
Code:
grep apple fruitsfile | awk '{FS=":";print "Fruit: "$1"\nColor: "$2"\nPrice: "$3"\nOrigin: "$4}'

# 4  
Old 05-10-2013
Genius! Just what the doctor ordered, perfect. I appreciate the help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep solutions tab-delimited file

Hello, I am trying to find a solution to problem that's proving to be beyond my newbie skills. The below files comes from a genetics study. File 1 describes a position on the genome and file 2 does the same but is formatted differently and has more information. I am trying to match all lines in... (5 Replies)
Discussion started by: andmal
5 Replies

2. Shell Programming and Scripting

How to add the line to previous line in | delimited text?

Hi All, I am new to Unix and I have one challenge and below are the details. I have pipe delimited text file in that data has span into multiple lines instead of single line. Sample data. Data should be like below for entire file. 41|216|398555|77|provided complete NP outcome data ... (21 Replies)
Discussion started by: Narasimhasss
21 Replies

3. UNIX for Dummies Questions & Answers

To find and display the middle line in a file using single line command.

Hi all, How can i display the middle line of a file using a single line command? (6 Replies)
Discussion started by: Lakme Pemmaiah
6 Replies

4. UNIX for Advanced & Expert Users

Help in adding a string at the end of each line and append files vertically

hi, i need a help in the script , need to append a string at the end of each line of a files , and append the files into a single file vertically. eg file1 has the following columns abc,def,aaa aaa,aa,aaa files 2 has the following rows and columns abc,def,aaa aaa,aa,aaa i... (3 Replies)
Discussion started by: senkerth
3 Replies

5. Shell Programming and Scripting

Grep:pipe delimited output

Hi, I am trying to search for a string in a file and print all the matched lines as pipe delimited format. My command is cat m_gid_trans.XML|grep -i '<TABLEATTRIBUTE NAME ="Lookup cache directory name"' The output I am getting is <TABLEATTRIBUTE NAME ="Lookup cache directory name"... (4 Replies)
Discussion started by: sampoorna
4 Replies

6. Shell Programming and Scripting

Finding a "word" through grep but display line above?

Hi guys. I am trying to perform a search using grep. I get my grep to work, but need to "awk" a Process Number that is 2 lines above... Example: I run a query on my TSM server for Processes that are "Waiting" for something...it returns this: Process Number: 32,881 Process... (14 Replies)
Discussion started by: Stephan
14 Replies

7. UNIX for Advanced & Expert Users

Grep in delimited file

Here is my file : (pipe delimited file) abc|abc|11112|ffff|3333|oo abc|abc|11112|ffff|3333|oo abc|11112|ffff|2222|oo abc|abc|11112|ffff|3333|oo abc|22222|ffff|1111|oo I need 2 things : 1. Need to grep for all rows where in 5th column is 3333. (i can do this using awk.. but my actual... (5 Replies)
Discussion started by: vinithepoo
5 Replies

8. Shell Programming and Scripting

4 GB delimited-textfile on ONE LINE

I have delimited-text files ( > 4GB ) and is just one line. OS: HP-UX 11.23 Awk / cut / sed all have line_max limitations. & unable to read one line in (Buffered-mode). Sample file: xxxx|adsfadf|Afdsa|adsf|afds|Asdfas|ads|Afds|Asdf| .....till forever, I want to put a carriage... (5 Replies)
Discussion started by: magedfawzy
5 Replies

9. Shell Programming and Scripting

Grep for NULL in a pipe delimited file

hi, How can I check for a field in a pipe-delimited file having a NULL value in Unix using a grep command or any other command. Please reply (5 Replies)
Discussion started by: sureshg_sampat
5 Replies

10. Shell Programming and Scripting

Make grep -c display like grep -n?

Hey Guys, Wondering if there is a way to do the following I have a file called test.txt abc def abc abc def I have a pattern file called pattern.txt containing the following abc def I want to do a count, but have it display the count value preceeding each line like grep -n (2 Replies)
Discussion started by: Jerrad
2 Replies
Login or Register to Ask a Question