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:

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

Hmm, normally an empty line gives more processing problems than no line.
Perhaps you can store in a variable and echo it:

result=$(
awk -F ";" '{print $19}' /Users/jb/Desktop/ReorderTempTotal.csv  | grep -o "\d.*"
)
echo "$result"

What MadeInGermany suggested is the best, just store result in variable, meanwhile you can try below one too

[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

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 )

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

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

awk -F ";" '{print match($19,/[0-9]+.*/)?substr($19,RSTART,RLENGTH):""}' /Users/jb/Desktop/ReorderTempTotal.csv

Well.. this works! Almost, because it's inserting 2 lines instead of 1
and when I use

awk -F ";" '{print match($19,/[0-9]+.*/)}' /Users/jb/Desktop/ReorderTempTotal.csv

it inserts a zero
It is promising though :slight_smile:

Use this

awk -F ";" '{print match($19,/[0-9]+.*/)?substr($19,RSTART,RLENGTH):""}' /Users/jb/Desktop/ReorderTempTotal.csv 

Great! This is working!
Thanks to you all guys!