Documenting files with sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Documenting files with sed
# 1  
Old 07-11-2017
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!
# 2  
Old 07-11-2017
Where would you locate the "first left parenthesis"?
This User Gave Thanks to RudiC For This Post:
# 3  
Old 07-11-2017
Quote:
Originally Posted by f77hack
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
This User Gave Thanks to bakunin For This Post:
# 4  
Old 07-11-2017
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)  }

This User Gave Thanks to RudiC For This Post:
# 5  
Old 07-12-2017
Quote:
Originally Posted by RudiC
Where would you locate the "first left parenthesis"?
yes sorry, my mistake, right parenthesis.
# 6  
Old 07-12-2017
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

This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. Web Development

Documenting Installation Problem with vue-beautiful-chat

REF: https://github.com/mattmezza/vue-beautiful-chat $ git clone https://github.com/mattmezza/vue-beautiful-chat.git Cloning into 'vue-beautiful-chat'... remote: Enumerating objects: 534, done. remote: Total 534 (delta 0), reused 0 (delta 0), pack-reused 534 Receiving objects: 100%... (2 Replies)
Discussion started by: Neo
2 Replies

2. Shell Programming and Scripting

Script Help -- documenting specific users that log into server

Hello All, I am trying trying to write a shell script that will do a couple things: 1.) Identify any username that logs into the server. 2.) When the user logs out, send them an email detailing their log in/out times, duration logged in, and what processes they ran. Basically,... (3 Replies)
Discussion started by: SecureScript
3 Replies

3. Programming

Documenting code

I am programming using C++ and am wondering how to comment my project. For example, when I declare a class I created a header with some description, then have the declaration stage, which I don't comment. Then when I define the actual functions I will put details about it, what it does,... (2 Replies)
Discussion started by: kristinu
2 Replies

4. UNIX for Dummies Questions & Answers

Any easy way of documenting cron entries?

I've been asked to do a high level summary of the cron jobs which run against a number of systems (to understand the potential scope for rewriting some of our core systems)...and was wondering how people have done this in the past. I've got approx 400 cron entries to go through. I will have... (2 Replies)
Discussion started by: GavP
2 Replies

5. UNIX for Dummies Questions & Answers

Documenting a Shell Script

I've been working on a very long shell script that's becoming a mini-application. It is my first script, and continues to grow each week, becoming more and more complex. I've been asked to document my script, beginning with basic information and then detailing any information about its... (1 Reply)
Discussion started by: yongho
1 Replies
Login or Register to Ask a Question