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 07-11-2017
f77hack f77hack is offline
Registered User
 
Join Date: Jan 2016
Last Activity: 22 August 2017, 7:02 AM 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 07-11-2017
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 26 September 2017, 11:01 AM EDT
Location: Aachen, Germany
Posts: 11,379
Thanks: 299
Thanked 3,529 Times in 3,246 Posts
Where would you locate the "first left parenthesis"?
The Following User Says Thank You to RudiC For This Useful Post:
f77hack (07-12-2017)
Sponsored Links
    #3  
Old Unix and Linux 07-11-2017
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 26 September 2017, 7:40 AM EDT
Location: In the leftmost byte of /dev/kmem
Posts: 5,566
Thanks: 106
Thanked 1,573 Times in 1,163 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 (07-12-2017)
    #4  
Old Unix and Linux 07-11-2017
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 26 September 2017, 11:01 AM EDT
Location: Aachen, Germany
Posts: 11,379
Thanks: 299
Thanked 3,529 Times in 3,246 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 (07-12-2017)
Sponsored Links
    #5  
Old Unix and Linux 07-12-2017
f77hack f77hack is offline
Registered User
 
Join Date: Jan 2016
Last Activity: 22 August 2017, 7:02 AM 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 07-12-2017
MadeInGermany MadeInGermany is online now Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 26 September 2017, 3:51 PM EDT
Location: Simplicity
Posts: 3,750
Thanks: 307
Thanked 1,258 Times in 1,137 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 (07-12-2017)
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 03:58 PM.