PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, November 19, 2022

[FIXED] How do I extract the string that follows another string from multiple files?

 November 19, 2022     bash, linux, scripting, sed     No comments   

Issue

I have a lot of files in the folder filesToCheck, some examples given below. I need the output result.txt as also shown below. I think the regex CAKE_FROSTING\(\".*\" is needed somehow for this task, but I am not well versed in bash scripting. I can use linux bash with any commands that do not require extra installations.

file1.cpp

something
CAKE_FROSTING("is.simply.the.best", "[no][matter][what]") { DO(something(0) == 1); }

file2.h

something else
CAKE_FROSTING(
"is.kinda.neat", 
"[i][agree]") something else
something more

file3.cpp

hello

file4.cpp

random_text CAKE_FROSTING("Can be nice") "more random text"

CAKE_CREAM("totally.sucks", "[trust][me]")

fileEmpty.h


result.txt

is.simply.the.best
is.kinda.neat
Can be nice

Edit: I tried

awk '"CAKE_FROSTING\("{print $2}' filesToCheck/file1.cpp

but this gives the wrong output "[no][matter][what]") and only runs on one file.


Solution

Using GNU sed

$ sed -Enz 's/.*CAKE_FROSTING\(\n?"([^"]*).*/\1\n/p' /path/to/filesToCheck/*
is.simply.the.best
is.kinda.neat
Can be nice


Answered By - HatLess
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing