Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unix cut command for empty string

Tags:

string

unix

cut

I'm trying to use cut -f to get desired part of the following:

""
""
""HTTP/1.1""200""OK""

In order to get HTTP/1.1, I'll need to do:

cut -f 3,3 -d '"'

Result will be:

empty
empty
HTTP/1.1

So first two lines will be empty, (I use "empty" to indicate that)

But I would need to keep the empty string:

""
""
HTTP/1.1

I guess I need to introduce if-else, that if original line is empty then stop cut, just keep ""? But how can I do this using one-line bash?

Thanks

======= A followup question: How to change

,,,

to

,"","",""

s/^$/\"\"/g does not work here.

like image 383
LookIntoEast Avatar asked Nov 22 '25 17:11

LookIntoEast


1 Answers

You can pipe the result of your cut command to:

sed s/^$/\"\"/g 

It will replace the empty lines with "".

That is:

cut -f 3,3 -d '"' | sed s/^$/\"\"/g
like image 173
ネロク・ゴ Avatar answered Nov 24 '25 10:11

ネロク・ゴ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!