Score:0

I need to insert 12spaces. It should match a particular string then go to next line with 12spaces

ca flag

I have a file called space.txt. I am doing the string match with ath2. I need to insert a string "newdata" after 12 spaces. I tried the below command, which works, but used manual white 12 spaces. How it can be achieved in another way?

sed -e "/ath2/a\            newdata" space.txt

I need output like this

unix@BDC8-LX-CVZ52X2:~/12space$ cat space
ath1
ath2
            newdata
ath3
ath4
ath5
David avatar
cn flag
Sounds a lot like a homework question.
ca flag
It is a requirement. I have tweaked the data to keep it simple. I am using the command to which works in a way but i don't want to go that way as it is not standard. If you have understood the question, can you please help me ?
pLumo avatar
in flag
Which Ubuntu version is that?
pLumo avatar
in flag
I think you should not try to keep it too simple, as we don't really see the benefits of it. You should explain in the question why you want to do it, so that we can give a proper answer. E.g. you can easily create n spaces e.g. with `printf`, but for your example that would be more complicated then just writing spaces inside `sed`.
Score:2
in flag

You can use printf to generate spaces:

sed -e '/ath2/a\'"$(printf %012s)"'newdata' "space.txt"
Score:1
bd flag

Is this what you want:

sed "/ath2/a$(printf '\%12s')newdata" space
pLumo avatar
in flag
isn't this practically the same as my answer?
Wayne Vosberg avatar
bd flag
Yes - I guess you hit post first.
ca flag
@Wayne: Exactly. This is what what i wanted. Thank you very much. sed "/ath2/a$(printf '\%12s')newdata" space this solved my issue.
ca flag
@pLumo: Actual requirement is i need to insert 'SystemCgroup=true' to the toml file as am setting cgroupdriver to systemd. It requires indentation , it should match for the string 'runc.options' and go to next line with 12spaces. What Wayne has given is perfectly matching for my requirement. Thanks alot!!
pLumo avatar
in flag
which is exactly the same as my answer but 2 minutes later...
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.