LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 07-01-2012, 08:49 PM   #1
zwiebelchen
LQ Newbie
 
Registered: Feb 2011
Posts: 7

Rep: Reputation: 0
sed using brackets to copy sth


Hi,

I have a file with lots of lines like
Quote:
else if (strncmp(input, "A1_ON", 5) == 0) { sendToUSB(dev_handle, 0); }
else if (strncmp(input, "A2_ON", 5) == 0) { sendToUSB(dev_handle, 0); }
else if (strncmp(input, "A3_ON", 5) == 0) { sendToUSB(dev_handle, 0); }
and I want to replace the last zero with the text between the "-Characers.

E.g.:
Quote:
else if (strncmp(input, "A1_ON", 5) == 0) { sendToUSB(dev_handle, 0); }
should become
Quote:
else if (strncmp(input, "A1_ON", 5) == 0) { sendToUSB(dev_handle, A1_ON); }
So, how to do ?

Thanks in advance,

zwiebelchen
 
Old 07-01-2012, 11:39 PM   #2
dru8274
Member
 
Registered: Oct 2011
Location: New Zealand
Distribution: Debian
Posts: 105

Rep: Reputation: 37
Using sed...
Code:
$ cat >data1.dat <<EOF
else if (strncmp(input, "A1_ON", 5) == 0) { sendToUSB(dev_handle, 0); }
else if (strncmp(input, "A2_ON", 5) == 0) { sendToUSB(dev_handle, 0); }
else if (strncmp(input, "A3_ON", 5) == 0) { sendToUSB(dev_handle, 0); }
EOF

$ sed -n 's/^\([^"]\+"\)\([^"]\+\)\(",.\+dev_handle, \)0\().\+\)$/\1\2\3\2\4/p' data1.dat
else if (strncmp(input, "A1_ON", 5) == 0) { sendToUSB(dev_handle, A1_ON); }
else if (strncmp(input, "A2_ON", 5) == 0) { sendToUSB(dev_handle, A2_ON); }
else if (strncmp(input, "A3_ON", 5) == 0) { sendToUSB(dev_handle, A3_ON); }
Happy with solution ... mark as [SOLVED]
 
Old 07-02-2012, 04:24 AM   #3
zwiebelchen
LQ Newbie
 
Registered: Feb 2011
Posts: 7

Original Poster
Rep: Reputation: 0
Thanks, worked great !
 
Old 07-02-2012, 07:02 PM   #4
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Stretch
Posts: 612

Rep: Reputation: 125Reputation: 125
I looked at this this question and thought about multiple back-references. I thought there may have been a simpler solution.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
using sed replace command with HTML brackets casperdaghost Linux - Newbie 1 02-25-2012 04:36 PM
Sed and brackets unihiekka Linux - Newbie 9 10-09-2010 11:08 PM
[SOLVED] copy lines from multiple files to one file using sed -w cruzdelsur Programming 12 08-26-2010 08:24 AM
Script: SED for Copy/Paste Lines from Files unihiekka Programming 2 10-07-2008 06:12 AM
Copy from file1 to file2 - Trying sed LinuxCrayon Linux - Newbie 3 03-30-2008 07:51 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration