Pi Horse
Pi Horse

Reputation: 2440

Script to extract characters from a file

I have a text file which looks like this :

/* 0 */
{
    "_id" : ObjectId("abcd")
}

/* 1 */
{
    "_id" : ObjectId("432432")
}

/* 2 */
{
    "_id" : ObjectId("32132121")
}

/* 3 */
{
    "_id" : ObjectId("321312")
}

I want to extract only the ObjectId from the file.

My output should be something like this :

abcd
432432
32132121
321312

What would be the correct shell script for it if I were to use grep or awk ?

Upvotes: 1

Views: 99

Answers (6)

NeronLeVelu
NeronLeVelu

Reputation: 10039

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

Based on the sample

Upvotes: 0

potong
potong

Reputation: 58488

This might work for you (GNU sed):

sed -n '/.*ObjectId("\([^"]*\)").*/s//\1/p' file

Upvotes: 0

BMW
BMW

Reputation: 45293

Using sed.

sed -n '/ObjectId/ s/.*"\(.*\)")/\1/p' file

Upvotes: 2

Jotne
Jotne

Reputation: 41460

Using awk

awk -F\" '/ObjectId/ {print $4}' file
abcd
432432
32132121
321312

If line has more than one data in the same line you can use this gnu awk (Due to RS)

cat file
/* 0 */
{
    "tac" : TacID("data") "_id" : ObjectId("abcd")
}

/* 1 */
{
    "_id" : ObjectId("432432") "tac" : TacID("data")
}

/* 2 */
{
    "_id" : ObjectId("32132121")
}

/* 3 */
{
    "_id" : ObjectId("321312")
}

awk -F\" 'NR>1 {print $2}' RS="ObjectId" file
abcd
432432
32132121
321312

Another gnu awk (Due to gensub) That read correct field if multiple field in same line.

awk '/ObjectId/ {print gensub(/.*ObjectId[(]"([^"]+).*/,"\\1","g")}' file

Upvotes: 1

klarki
klarki

Reputation: 915

another way you can do it in gawk:

gawk 'match($0, /ObjectId\("(.*)"\)/, res) {print res[1]}' file

Upvotes: 1

Kent
Kent

Reputation: 195209

this does the job:

grep -Po ' ObjectId\("\K[^"]*' file

if you love awk:

awk -F' ObjectId\\("' '{sub(/".*$/,"",$2)}$0=$2' file

Upvotes: 6

Related Questions