Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Documenting files with sed

Shell Programming and Scripting


Tags
parse, sed

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 2 Weeks Ago
f77hack f77hack is offline
Registered User
 
Join Date: Jan 2016
Last Activity: 12 July 2017, 5:26 PM EDT
Location: 3rd planet
Posts: 21
Thanks: 15
Thanked 1 Time in 1 Post
Documenting files with sed

All,
I am looking for a way to improve this small one liner.

one of the files has this kind of format

Code:
public func <<<T>(array: [T], offset: Int) -> [T] { return array.shiftedLeft(by: offset)  }


Code:
gsed -i '/public func/i /\/\/\public func \n/\/\==mark==public func' t

results in

Code:
///public func 
//==mark==public func
public func <<<T>(array: [T], offset: Int) -> [T] { return array.shiftedLeft(by: offset)  }

i'd like the code to quickly parse to grab all of the string until it the first left parenthesis.


Code:
///public func <<<T>(array: [T], offset: Int) 
//==mark==public func <<<T>(array: [T], offset: Int) 
public func <<<T>(array: [T], offset: Int) -> [T] { return array.shiftedLeft(by: offset)  }

Thanks!
Sponsored Links
    #2  
Old Unix and Linux 2 Weeks Ago
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 25 July 2017, 5:19 PM EDT
Location: Aachen, Germany
Posts: 11,063
Thanks: 284
Thanked 3,409 Times in 3,139 Posts
Where would you locate the "first left parenthesis"?
The Following User Says Thank You to RudiC For This Useful Post:
f77hack (1 Week Ago)
Sponsored Links
    #3  
Old Unix and Linux 2 Weeks Ago
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 25 July 2017, 5:40 PM EDT
Location: In the leftmost byte of /dev/kmem
Posts: 5,487
Thanks: 104
Thanked 1,528 Times in 1,133 Posts
Quote:
Originally Posted by f77hack View Post
i'd like the code to quickly parse to grab all of the string until it the first left parenthesis.
judging from your example outcome you perhaps mean right bracket yes:


Code:
///public func <<<T>(array: [T], offset: Int) 
//==mark==public func <<<T>(array: [T], offset: Int) 
public func <<<T>(array: [T], offset: Int) -> [T] { return array.shiftedLeft(by: offset)  }

Try this - i have made your one-liner a multi-liner, btw., because it is easier to keep track of. You can convert it into a - quite unreadable - one-liner again, if you insist in unintelligible code. ;-)


Code:
sed '/public func/ {
                    h
                    s/^[[:blank:]]*//\/\/\//
                    s/\([^)]*)\).*/\1/p
                    s/^\/\/\//\/\/==mark==/p
                    g
               }' /path/to/file

Here is a version with commentary:


Code:
sed '/public func/ {                          # for every line with "pub..." do:
               h                              # copy line to hold space
               s/^[[:blank:]]*//\/\/\//       # replace leading blanks (if any) with three slashes
               s/\([^)]*)\).*/\1/p            # remove everything after first ")" and print the result
               s/^\/\/\//\/\/==mark==/p       # replace leading "///" with "//==mark==" and print again
               g                              # copy hold space (original line) back to pattern space
     }'

Notice that "sed -i" is a GNU-speciality and is not recommended to use. Instead write to a temporary file and move this over the original (once you are satisfied). sed -i does the same, but behind your back and in case something goes wrong you have less options to troubleshoot that.

I hope this helps.

bakunin
The Following User Says Thank You to bakunin For This Useful Post:
f77hack (1 Week Ago)
    #4  
Old Unix and Linux 2 Weeks Ago
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 25 July 2017, 5:19 PM EDT
Location: Aachen, Germany
Posts: 11,063
Thanks: 284
Thanked 3,409 Times in 3,139 Posts
Right parenthesis seems better, thanks Bakunin. Try also

Code:
sed 's/public func[^)]*)/\/\/&\n\/\/==mark==&\n&/' file
//public func <<<T>(array: [T], offset: Int)
//==mark==public func <<<T>(array: [T], offset: Int)
public func <<<T>(array: [T], offset: Int) -> [T] { return array.shiftedLeft(by: offset)  }

The Following User Says Thank You to RudiC For This Useful Post:
f77hack (1 Week Ago)
Sponsored Links
    #5  
Old Unix and Linux 1 Week Ago
f77hack f77hack is offline
Registered User
 
Join Date: Jan 2016
Last Activity: 12 July 2017, 5:26 PM EDT
Location: 3rd planet
Posts: 21
Thanks: 15
Thanked 1 Time in 1 Post
Quote:
Originally Posted by RudiC View Post
Where would you locate the "first left parenthesis"?
yes sorry, my mistake, right parenthesis.
Sponsored Links
    #6  
Old Unix and Linux 1 Week Ago
MadeInGermany MadeInGermany is offline Forum Advisor  
Registered User
 
Join Date: May 2012
Last Activity: 25 July 2017, 4:53 PM EDT
Location: Simplicity
Posts: 3,638
Thanks: 285
Thanked 1,217 Times in 1,100 Posts
With another separator we don't need to \/ each literal /
As classic multi-line (\ plus newline instead of gsed-only \n)

Code:
sed 's#public func[^)]*)#///&\
//==mark==&\
&#' t

The Following User Says Thank You to MadeInGermany For This Useful Post:
f77hack (1 Week Ago)
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Documenting code kristinu Programming 2 06-01-2011 03:23 PM
Any easy way of documenting cron entries? GavP UNIX for Dummies Questions & Answers 2 03-29-2007 10:27 AM
Documenting a Shell Script yongho UNIX for Dummies Questions & Answers 1 06-29-2005 01:49 PM



All times are GMT -4. The time now is 08:44 PM.