Multiple Results of Grep in one Line/String?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiple Results of Grep in one Line/String?
# 8  
Old 06-10-2016
@ andy391791: Did you run that scriptlet on the sample file in post#1? It will print exactly the desired fields. And, if extended by ORS=";", it will print the desired single line. The residual flaw is the trailing semicolon...
# 9  
Old 06-10-2016
Quote:
Originally Posted by andy391791
Hi Made in Germany, i looked at your response and im confused about the format of the statment

Code:
awk 'NR%2==0' RS='"' file

I understand its only printing even lines and the record separator is "'" but i dont understand the format.

If
Code:
RS='"'

is an action why isnt it surrounded by braces and why is it outside the quotes ?

Many thanks
The RS= is an initial variable assignment before opening the file.
The main loop only has the condition NR%2==0 where the default action is {print}.
This User Gave Thanks to MadeInGermany For This Post:
# 10  
Old 06-11-2016
Code

Not so easy in /bin/sh, but quite straightforward in awk (horses for courses):

Code:
#!/usr/bin/awk -f

BEGIN {
    FS=":"
}

{
    join()
    next
}

END {
    printf "\n"
}

function join() {
    gsub("\"","",$1)
    printf "%s%s", semi, $1
    semi = ";"
}

That will put it all on one line; if you want lines joined in batches of three, that is a little more work. The first step to solving a problem is specifying it precisely!
# 11  
Old 06-11-2016
Assuming that there is only one quoted string before the colon (as in the sample input), it is also easy with any POSIX-conforming shell:
Code:
#!/bin/ksh
while IFS='"' read junk data junk
do	printf '%s%s' "$semi" "$data"
	semi=';'
done < "file.txt"
echo

This was written using a Korn shell and tested with ksh and bash with both of them producing the requested results:
Code:
Word1;Word2;This is Word3

Running it with zsh also produced the same results even though zsh obviously does not conform to the standard's quoting requirements for a shell (as mentioned in post #6 in this thread).
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 for multiple string

im learning grep and i need some help. i have input word file like this fish map.this is go to.that is i want sed,awk or grep command to extract the following in this format someword SPACE someword.so output will be fish map. go to.Please use CODE tags as required by forum... (11 Replies)
Discussion started by: ahfze
11 Replies

2. Shell Programming and Scripting

Multiple results as input to grep or awk

Hi: This is my first post in this forum. apologies if I am asking question that has been asked here multiple times. I wrote a python script that prints list of identifiers. I have another file notoriously big and with no particular format. I plan to search the identifiers in this file... (2 Replies)
Discussion started by: oriolebaltimore
2 Replies

3. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

4. Shell Programming and Scripting

Grep a string from input file and delete next three lines including the line contains string in xml

Hi, 1_strings file contains $ cat 1_strings /home/$USER/Src /home/Valid /home/Review$ cat myxml <projected value="some string" path="/home/$USER/Src"> <input 1/> <estimate value/> <somestring/> </projected> <few more lines > <projected value="some string" path="/home/$USER/check">... (4 Replies)
Discussion started by: greet_sed
4 Replies

5. Shell Programming and Scripting

Grep a string and write a value to next line of found string

Hi, I have two variables x and y. i need to find a particular string in a file, a workflow name and then insert the values of x and y into the next lines of the workflow name. basically it is like as below wf_xxxxxx $$a= $$b= $$c= figo $$d=bentley i need to grep the 'wf_xxxx' and then... (6 Replies)
Discussion started by: angel12345
6 Replies

6. Shell Programming and Scripting

grep on string and printing line after until another string has been found

Hello Everyone, I just started scripting this week. I have no background in programming or scripting. I'm working on a script to grep for a variable in a log file Heres what the log file looks like. The x's are all random clutter xxxxxxxxxxxxxxxxxxxxx START: xxxxxxxxxxxx... (7 Replies)
Discussion started by: rxc23816
7 Replies

7. UNIX for Dummies Questions & Answers

Displaying the Second Line of the Grep Search Results

Hi I really hope someone can help with the below question. Lets say that I have a file called output.txt and I want to display all of the lines which contain the word ‘disconnect'. I know that this can easily be obtained by using the following command: grep -i disconnect output.txt However,... (6 Replies)
Discussion started by: Sunny Sid
6 Replies

8. Shell Programming and Scripting

Grep a string and print a string from the line below it

I know how to grep, copy and paste a string from a line. Now, what i want to do is to find a string and print a string from the line below it. To demonstrate: Name 1: ABC Age: 3 Sex: Male Name 2: DEF Age: 4 Sex: Male Output: 3 Male I know how to get "3". My biggest problem is to... (4 Replies)
Discussion started by: kingpeejay
4 Replies

9. Shell Programming and Scripting

Multiple Grep Results - Formatting

Hello, Perhaps someone here can help with this. I'd like to grep a plain text file for a word and output each line containing a word found to a seperate line instead of back to back. Examples: Basic command: cat file.txt > grep -i CAT > results.txt file.txt: The cat said meow The... (7 Replies)
Discussion started by: sysera
7 Replies

10. Shell Programming and Scripting

diffrent results between command line and scripted grep

When I type a command at the command line it supplies one result and the exact same command in a script egrep '^01|^02|^03|^04' file > fileout count = 29353 same count in the script yields a count of 23492 is there any reason this could be happening. (1 Reply)
Discussion started by: r1500
1 Replies
Login or Register to Ask a Question