Visit Our UNIX and Linux User Community


Insert newline when grep result is empty


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert newline when grep result is empty
# 1  
Old 08-23-2016
Insert newline when grep result is empty

Given a csv file with 40 columns with name, address, hometown etc.
I use a bash command in 1 line which:
1. gets the address column and pipes that to
2. grep the first digit and everything that follows

Command:
Code:
awk -F ";" '{print $19}' /Users/jb/Desktop/ReorderTempTotal.csv  | grep -o "\d.*"

It generates the results I want, except in the case that the grep result is empty.
When there is no digit present, the command prints nothing and the result will end up with 1 line less. That gives a problem in further processing. I need the same number of lines as in the original file. So in that case I'd like to insert a new line for further processing.
And the code needs to be in one line
# 2  
Old 08-23-2016
Hmm, normally an empty line gives more processing problems than no line.
Perhaps you can store in a variable and echo it:
Code:
result=$(
awk -F ";" '{print $19}' /Users/jb/Desktop/ReorderTempTotal.csv  | grep -o "\d.*"
)
echo "$result"

# 3  
Old 08-23-2016
What MadeInGermany suggested is the best, just store result in variable, meanwhile you can try below one too
Code:
[akshay@localhost tmp]$ cat f
2
Haider
Bash

[akshay@localhost tmp]$ touch ff
[akshay@localhost tmp]$ wc -l ff
0 ff

[akshay@localhost tmp]$ cat f |  grep -o "\d.*"  || echo "newline"
der

[akshay@localhost tmp]$ cat ff |  grep -o "\d.*"  || echo "newline"
newline

So your command would be
Code:
awk -F ";" '{print $19}' /Users/jb/Desktop/ReorderTempTotal.csv |  grep -o "\d.*"  || echo

Note : grep's default mode is (iirc) POSIX regex, and \d is pcre. You can either pass -P to gnu grep, for perl-like regexps, or use [[:digit:]] instead of \d( BSD grep's -E mode includes \d )
# 4  
Old 08-23-2016
Sadly, both are no working

@MadeInGermany
I need this result with blank lines because the result should match with the original cvs. Otherwise data from different lines get mixed up.
The resulting data wil be used for a new column added to the original

I added the cvs I use for the testing
ReorderTempTotal.csv.zip
# 5  
Old 08-23-2016
Quote:
Originally Posted by JBVeenstra
Sadly, both are no working

@MadeInGermany
I need this result with blank lines because the result should match with the original cvs. Otherwise data from different lines get mixed up.
The resulting data wil be used for a new column added to the original

I added the cvs I use for the testing
Attachment 6770
What is expected output for the given sample ?
In case if you expect newline to be printed for the line which does not content digits, you may try below command
Code:
awk -F ";" '{print match($19,/[0-9]+.*/)?substr($19,RSTART,RLENGTH):""}' /Users/jb/Desktop/ReorderTempTotal.csv


Last edited by Akshay Hegde; 08-23-2016 at 11:48 AM.. Reason: Forgot to remove RS while posting
# 6  
Old 08-23-2016
Well.. this works! Almost, because it's inserting 2 lines instead of 1
and when I use
Code:
awk -F ";" '{print match($19,/[0-9]+.*/)}' /Users/jb/Desktop/ReorderTempTotal.csv

it inserts a zero
It is promising though Smilie
# 7  
Old 08-23-2016
Quote:
Originally Posted by JBVeenstra
Well.. this works! Almost, because it's inserting 2 lines instead of 1
and when I use
Code:
awk -F ";" '{print match($19,/[0-9]+.*/)}' /Users/jb/Desktop/ReorderTempTotal.csv

it inserts a zero
It is promising though Smilie
Use this
Code:
awk -F ";" '{print match($19,/[0-9]+.*/)?substr($19,RSTART,RLENGTH):""}' /Users/jb/Desktop/ReorderTempTotal.csv


Previous Thread | Next Thread
Test Your Knowledge in Computers #695
Difficulty: Easy
The Cathedral and the Bazaar: Musings on Linux and Open Source by an Accidental Revolutionary (abbreviated CatB) is an essay, and later a book, written by Alfred Hitchcock on software engineering methods.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert a newline after match in files of specific name under some subdirectories?

Hi I'd like to add the newline: \tuser: nobody", or "<TAB>user: nobody to all files named: docker-compose.ymlin subfolders of pwd with names beginning with 10-20. Within these files, I'd like to find the line (there'll only be one) containing: command: celery workerNOTE: As far as... (2 Replies)
Discussion started by: duncanbetts
2 Replies

2. UNIX for Dummies Questions & Answers

Insert row into empty file...how?

Greetings: I generate an empty flat file just fine when there's no data returned from my process, as the customer wants one always (using the 1st line of the below script). However, they also want at least the column names in this flat file (row 1, the only row to be in the emply file). I'm... (7 Replies)
Discussion started by: Benrosa
7 Replies

3. Shell Programming and Scripting

Insert a newline in XML

Hi guys, I got a requirement that ... (5 Replies)
Discussion started by: mohanalakshmi
5 Replies

4. Solaris

grep result in newline

Hi While trying to do a search on solaris, the grep results seems to be appearing on the same line instead of the new line. Wed Jan 18 14:45:48 weblogic@test:/abcd$ grep qainejb02 * qa_cluster_biz_view_tc_intl_servers_ports_2:qainejb02 7101 qa_cluster_servers_2:qainejb02... (2 Replies)
Discussion started by: ganga.dharan
2 Replies

5. Shell Programming and Scripting

sed insert text without newline

Hi, I use sed to insert text at beginning of a file. But sed inserts a newline after my text that I do not need. For example, I want to insert "foo" at the beginning of my file: > cat myfile This is first line. > sed -i '1i\foo' myfile > cat myfile foo This is first line. ... (5 Replies)
Discussion started by: tdw
5 Replies

6. Shell Programming and Scripting

AWK Script Issue insert newline for a regular expression match

Hi , I am having an issue with the Awk script to insert newline for a regular expression match Having a file like this FILE1 #################### RXOER , RXERA , RXERC , RXERD .RXEA(RXBSN), RXERD , REXCD input RXEGT buffer RXETRY ####################### Want to match the RXE... (38 Replies)
Discussion started by: jaita
38 Replies

7. Shell Programming and Scripting

How to check a variable for empty and a newline

I have a variable with a new line. I want to check this variable for empty or a new line Can anyone advise (4 Replies)
Discussion started by: Muthuraj K
4 Replies

8. Shell Programming and Scripting

Insert two newline after some pattern

Hi, I need to insert two newline characters after matching of a pattern in each line of a file. Eg. If i have a file with contents as follows:- Now, i want output as follows :- i.e., I need to insert two newline characters after the occurance of pattern "</Message>>". Thnx... (1 Reply)
Discussion started by: DTechBuddy
1 Replies

9. Shell Programming and Scripting

use regexp to insert newline within a line

I have successfully used regexp and sed to insert a newline before or after a line containing a matched pattern /WORD/. However, I want to insert a newline immediately following /WORD/ and not after the -line- containing the pattern matched. I can match a pattern, but it is matched via a wild card... (2 Replies)
Discussion started by: kpeirce
2 Replies

10. Shell Programming and Scripting

how to keep newline characters in command execution result?

I found that when I used a variable to receive the result from a command execution, the newline characters were removed from the variable. For example, I ran $ ret=`ls -l` $ echo $ret Then, I saw: total 40 -rw-r--r-- 1 testtrunk testtrunk 0 Dec 13 11:13 pk -rw-rw-r-- 1 testtrunk... (2 Replies)
Discussion started by: pankai
2 Replies

Featured Tech Videos