Narain
Narain

Reputation: 5421

Extract a string from all lines in a file

I want to extract a string which has a unique prefix from all lines in a file. Though I am bad at string manipulations and regular expressions, I tried to use sed, cut commands but failed to extract the string.

My Sample file looks soemthing like

string1 string2 PREFIX_some_string1 string3 string4
string5 string6 PREFIX_some_string2 string7 string8
string9 string10 PREFIX_some_string3 string11 string12
string13 string14 PREFIX_some_string4 string15 string16

I just want to extract PREFIX_some_string as a whole as

PREFIX_some_string1
PREFIX_some_string2
PREFIX_some_string3
PREFIX_some_string4

What could be the command ?

Upvotes: 0

Views: 114

Answers (5)

Juan Diego Godoy Robles
Juan Diego Godoy Robles

Reputation: 14945

Just a gawk command:

$ gawk 'match($0, "(PREFIX_[^[:blank:]]+)", a) {print a[1]}' file

Upvotes: 1

Sivasakthi Jayaraman
Sivasakthi Jayaraman

Reputation: 4724

Another solution using cut command
Cut:
-d delimiter as space
-f column to be printed

$ cut -d " " -f3 sample.txt

PREFIX_some_string1
PREFIX_some_string2
PREFIX_some_string3
PREFIX_some_string4

Upvotes: 0

Jotne
Jotne

Reputation: 41446

For the sample file, this will do:

awk '/PREFIX/' RS=" " file
PREFIX_some_string1
PREFIX_some_string2
PREFIX_some_string3
PREFIX_some_string4

This will pint the PREFIX section at any location on the line.

Upvotes: 1

NeronLeVelu
NeronLeVelu

Reputation: 10039

sed -n 's/.* \(PREFIX_[^ ]*\).*/\1/p' YourFile

Assuming space char is the field/string separator and no other PREFIX_ are possible in structure

sed -n 's/\([^ ]\{1,\} \{1,\}\)\{2\}\(PREFIX_[^ ]*\).*/\1/p' YourFile

More accurate version to take only PREFIX_ as 3th field

Upvotes: 1

fedorqui
fedorqui

Reputation: 289495

You can use grep:

$ grep -o 'PREFIX[^ ]*' file
PREFIX_some_string1
PREFIX_some_string2
PREFIX_some_string3
PREFIX_some_string4

This greps for matches of PREFIX + whatever characters coming until a space is found. It just prints the match because we are using the -o option in grep: Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.

Upvotes: 2

Related Questions