1

I have a text file with data as:

(832555,488012,0,17:31:32.541,2014-08-06 17:31:32.000,0,0,NULL,FBCD,"-6484620512517810993"etcetcetc

I want to extract the string post FBCD so my output should be:

FBCD,"-6484620512517810993"etcetc

I am able to find the position of FBCD using awk:

awk '{print substr("FBCD",1,200)}' file.txt

but I cannot extract the remaining values.

1
  • 1
    Substr doesnt work like that Commented Sep 2, 2014 at 12:30

2 Answers 2

3

USing awk With substr

awk '{print substr($0,index($0,"FBCD"),200)' file

Using sed

sed -e 's/^.*\(FBCD.\{200\}\).*/\1/' file

If you want it till the end of the line

    awk '{print substr($0,index($0,"FBCD"))' file

    sed -e 's/^.*\(FBCD\)/\1/' file
Sign up to request clarification or add additional context in comments.

2 Comments

+1 but I don't think the ,200 is useful. I think the OP was using that as just some large value to get the rest of the line because he didn't know that's what he'd get by just leaving that argument empty.
@EdMorton Edited in case that is what he wanted.
1

Your codes: substr("FBCD",1,200)

will cut the input string from char 1 - 200. but you gave FBCD as input string, and FBCD has only length 4, that's why you got only FBCD.

In fact, grep was born to extract things, would this help you?

grep -oE 'FBCD.{1,196}' file

6 Comments

"grep was born to extract strings" - that may be debatable since the -o option to grep that you're using to extract is not in POSIX
@kent you have used a backtick instead of a quote
@Jidder oh, yes, I didn't test, was a typo, confused with markdown code block.... thanks.
@1_CR is correct, grep was born to print the line containing a regexp (g/re/p). These days the GNU guys have their version of it recursively searching for files, cooking breakfast and all sorts of other things. I'm on the fence about whether -o was a good idea but at least it's not obviously a bad idea (unlike trying to do finds job).
@EdMorton so I should say grep was born to extract lines.. :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.