1

How can I remove characters of text in one line before a specific character?

I want to remove characters to the left of the first letter "F"

Example:

null###F01|54646466|00K4234001|IFD|1990101
null###F02|54646499|00K4234001|TFS|1990101
null###F03|03232432|00RWEREW01|ZAI|1990101

The result must be

F01|54646466|00K4234001|IFD|1990101
F02|54646499|00K4234001|TFS|1990101
F03|03232432|00RWEREW01|ZAI|1990101

I 'm trying with awk -F"F" '{print "F"$2}' but it does not work.

4
  • ...why is IFD made IFS?
    – mikeserv
    Jul 17 '15 at 22:38
  • IFS is a data example, Im corrected data Jul 17 '15 at 22:45
  • 2
    sed 's/^[^F]*//' datafile
    – roaima
    Jul 17 '15 at 22:53
  • 1
    What about reading the man page or any of the gazillon blog posts about that?
    – user123418
    Jul 18 '15 at 1:29
7

If your input all looks like that, then the most efficient solution is most likely:

cut -d\# -f4- <<\IN
null###F01|54646466|00K4234001|IFD|1990101
null###F02|54646499|00K4234001|TFS|1990101
null###F03|03232432|00RWEREW01|ZAI|1990101
IN

I use a heredocument above to demonstrate, but you can just use cut -d\# -f4- <infile >outfile. You could also use cut -c8- <in >out (which, if I'm honest, is probably actually the most efficient way). Either way...

OUTPUT

F01|54646466|00K4234001|IFD|1990101
F02|54646499|00K4234001|TFS|1990101
F03|03232432|00RWEREW01|ZAI|1990101
2
  • cool (+1). Why <<\IN and not <<IN ?
    – JJoao
    Jul 18 '15 at 18:30
  • @JJao - because a quoted delimiter protects the contents against expansion. I guess it is irrelevant here, but it is a habit only not to quote when i definitely mean for something to get expanded.
    – mikeserv
    Jul 18 '15 at 18:33
1
grep -o 'F.*'

or (we need more than 30 chars)

perl -pe 's!.*?F!F!'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.