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

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 07-11-2017   -   Original Discussion by f77hack
f77hack's Unix or Linux Image
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   -   Original Discussion by f77hack
RudiC's Unix or Linux Image
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 23 January 2018, 8:59 AM EST
Location: Aachen, Germany
Posts: 11,980
Thanks: 356
Thanked 3,692 Times in 3,390 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   -   Original Discussion by f77hack
bakunin's Unix or Linux Image
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 22 January 2018, 12:33 PM EST
Location: In the leftmost byte of /dev/kmem
Posts: 5,678
Thanks: 112
Thanked 1,633 Times in 1,198 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   -   Original Discussion by f77hack
RudiC's Unix or Linux Image
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 23 January 2018, 8:59 AM EST
Location: Aachen, Germany
Posts: 11,980
Thanks: 356
Thanked 3,692 Times in 3,390 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   -   Original Discussion by f77hack
f77hack's Unix or Linux Image
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   -   Original Discussion by f77hack
MadeInGermany's Unix or Linux Image
MadeInGermany MadeInGermany is offline Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 23 January 2018, 6:57 AM EST
Location: Simplicity
Posts: 3,950
Thanks: 335
Thanked 1,325 Times in 1,195 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
Closed

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 04:23 PM
Any easy way of documenting cron entries? GavP UNIX for Dummies Questions & Answers 2 03-29-2007 11:27 AM
Documenting a Shell Script yongho UNIX for Dummies Questions & Answers 1 06-29-2005 02:49 PM



All times are GMT -4. The time now is 09:59 AM.