[SOLVED] Find some pattern from all files and remove certain characters from it.
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Find some pattern from all files and remove certain characters from it.
Kindly help me solving following problem. Remove all "\n" from Test_Macro in all files. Please see below example:
Fil1.txt
Code:
Test_Macro(abc, def, "\n string1 string2 \n test string",
"test string2 \n");
// Some code or text
Test_Macro(asdsadas, abc.str(), "test String1\n");
// Some code...
dir1/File2.txt
Code:
Test_Macro(abc, def, "\n string1 string2 \n test string",
"test string2 \n",
123456);
// Some code or text
Test_Macro(asdsadas, abc.str(), "test String1");
// Some code...
Expected Result:
File1.txt
Code:
Test_Macro(abc, def, " string1 string2 test string",
"test string2 ");
// Some code or text
Test_Macro(asdsadas, abc.str(), "test String1");
// Some code...
dir1/File2.txt
Code:
Test_Macro(abc, def, " string1 string2 test string",
"test string2 ",
123456);
// Some code or text
Test_Macro(asdsadas, abc.str(), "test String1");
// Some code...
Any help or suggestions are much appreciated. I am planning to write some script. Because I have many different types of files and many such macros. Thanks in advance!
Last edited by Rock26; 10-27-2016 at 04:14 PM.
Reason: Don't know how to submit code in this forum.
What effort have you made to this solution (apart from asking here of course)?
You mention writing a script ... what language will that be in?
Hi Grail. I can write bash script if I find some ways to achieve this result. I tried some commands. But I can't achieve final result. "sed 's/\\n//g'", "sed ':a;N;$!ba;s/[^;]\n[ ]*/ /g;' file1.txt | grep Test_Macro" and etc. etc. But this remove all backslashes and n characters. I am also newbie. So any suggestions are welcome.
Presuming normally formatted text, the sed 's/\\n//g' should work. Show us full commands and output of those snippets you posted above.
Hi syg00,
This command will remove all "\n". I just want to remove from Test_Macro. I have approx. 5-8 directories with many files in each. Simple example for one file:
File1.c
Code:
Test_Macro(abc, def, "\n string1 string2 \n test string",
"test string2 \n");
printf("Welcome to C world..\n");
// Some code or text
Test_Macro(asdsadas, abc.str(), "test String1\n");
Another_Macro("Done with this file...\n\n");
// Some code...
Expected result:
Code:
Test_Macro(abc, def, " string1 string2 test string",
"test string2 ");
printf("Welcome to C world..\n");
// Some code or text
Test_Macro(asdsadas, abc.str(), "test String1");
Another_Macro("Done with this file...\n\n");
// Some code...
Actual output:
Code:
>sed 's/\\n//g' File1.c
Test_Macro(abc, def, " string1 string2 test string",
"test string2 ");
printf("Welcome to C world.."); // removed "\n"
// Some code or text
Test_Macro(asdsadas, abc.str(), "test String1");
Another_Macro("Done with this file..."); // removed "\n"
// Some code...
Last edited by Rock26; 10-27-2016 at 06:26 PM.
Reason: Formatting..
Yeah, sorry about that, I realised after I'd posted ...
You were pretty close, but you neeed to limit the accumulation of lines based on "Test" and the semi-colon. Try this instead
Code:
sed '/^Test/ {:a;/;/!{N;$!ba};s/\\n/ /g;n};' test.file
Yeah, sorry about that, I realised after I'd posted ...
You were pretty close, but you neeed to limit the accumulation of lines based on "Test" and the semi-colon. Try this instead
Code:
sed '/^Test/ {:a;/;/!{N;$!ba};s/\\n/ /g;n};' test.file
Thanks a lot @syg00. This worked perfectly fine. Please suggest me some good "sed" and "regex" tutorial. Appreciate all your help and time.
I'm probably the wrong person to ask. I tend to see something and think "hmmm, that's an interesting way to do it", and go and and find out more. Quite often posts here on LQ start the process.
For sed that means "info sed" - the gnu.org sed homepage has a downloadable html version if you prefer. Also have a look at the sed1liners at sf.
For regex, that is a whole 'nuther problem - different implementations (posix, basic, extended, PCRE, ...). Again I don't use tutorials, I just bumble along till I figure it out.
Hopefully others will have more sensible suggestions.
The reason I avoided that sort of thing grail was the possible case of plain text (not semi-colon terminated) lines containing "\n".
Edge case maybe, but we never seem to get the full specs .... :shrug:
Ohh. I am using 4.2.1. I tried this for simple 5 lines. It only worked for first 3 lines. I think it doesn't accept ";" delimiter.
Code:
rock@test]$ sed --version
GNU sed version 4.2.1
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE,
to the extent permitted by law.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.