user902691
user902691

Reputation: 1159

replacment for sed "z"- line parsing

I have text file:

# How often update data
period=30
#For Location
location=London.uk

I want print on stdout:

London.uk

When i use sed s/".*location="/""/ "$file_name"

It work correct. But without option -z I have following message:

# How often update data period=30 #For Location London.uk

How print location without -z ?

Upvotes: 0

Views: 813

Answers (4)

Jotne
Jotne

Reputation: 41456

If you like to try awk

awk -F= '/^location/ {print $2}' "$file_name"
London.uk

Upvotes: 1

pfnuesel
pfnuesel

Reputation: 15310

I don't know what the -z option is, my man page doesn't know about it. But reading from your description, it's the same as the -n option, i.e. not printing the read lines. If you want to have a solution without -n, you can try

sed '/^location/!d; s/^location=//' "$file_name"

That deletes all lines that do not start with location and then in the line that starts with location, location= is removed.

Remains the question why you wouldn't want to use the -n option.

Upvotes: 1

Vijay
Vijay

Reputation: 67221

You can use perl as well for same:

perl -lne 'print $1 if(/^location=(.*)/)' your_file

If you insist on sed then you can go with:

sed -n 's/^location=\(.*\)/\1/p' your_file

Upvotes: 1

jkshah
jkshah

Reputation: 11703

Try following sed

sed -n 's/^location=//p' "$file_name"

I would have preferred using grep

grep -oP '^location=\K.*' "$file_name"

Upvotes: 1

Related Questions