ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I thought this would be fairly straightforward, but it's got me stumped. The following sed command will find every blank line and insert the new line "aaaa" after each one:
Code:
sed '/^$/i\
aaaa'
But can I modify it to insert the new line only once, after just the first blank line, no matter how many blank lines are in my file? Thanks in advance to all who respond.
Well, now.....that depends on the screw, doesn't it........ In my motorcycle repair days, we had a saying: "Get a bigger hammer". With what we were dealing with, that was the right answer surprisingly often.
Bash scripting is like having the world biggest collection of hammers---all sizes. We all have our favorite that we grab first.
I've not posted the palindrome problem officially, but have mentioned it several times. Basically, the challenge is to make a sed script that recognizes any palindrome and outputs Yes, if it is, and No if it is not.
The range is in the definition of a 2addr (which means 0, 1, or 2 addresses), and addresses take a couple of forms, such as line number, or a pattern. Example 2addr's are 4, or 3,4, or 5,/somepat/, or /pat2/,$, etc.
Can sed "nest"; sure. As long as you live within its grammer, you can do what you're asking about:
Code:
$ cat in
Section AAA
var1 = abba
var2 = baab
var3 = abab
EndSection
Section BBB
var1 = nisse
var2 = lasse
var3 = salle
EndSection
$ cat sed.script2
1,$ {
=
/Section/ {
p
/End/ {
a\
This is the end
}
}
}
$ sed -nf sed.script2 in
1
Section AAA
2
3
4
5
EndSection
This is the end
6
7
Section BBB
8
9
10
11
12
EndSection
This is the end
Exactly. Just think of 2addr or 1addr as asking "does the current line number or pattern (space) match the address(es) specified in 2addr or 1addr? ... If so, do stuff".
An answer to Mr. C.'s sed palindrome checker problem:-
Code:
#!/bin/bash
# If $1 is a palindrome output 'Yes'; otherwise output 'No'
echo $1 | sed 's/.*/&\n&\nNo\nYes/' | # Make 4 lines: $1/$1/No/Yes
sed '2 {/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.// }' | # 'rev' line 2
sed '$!N; /^\(.*\)\n\1$/!P; D' | # Emulate 'uniq'
sed -n '3p' # Output line 3
The method:-
Set up 4 lines,
$1
$1
No
Yes
Emulate 'rev' only on line 2 (e.g. "abcde" becomes "edcba")
Emulate 'uniq': if $1 is a palindrome line 2 is deleted so "Yes" moves up from line 4 to replace "No" on line 3.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.