user3038871
user3038871

Reputation: 51

Adding strings to certain lines using sed

I'm trying to do some formatting to some man pages and I need to add

] hfill \\

to certain headers. I want to make this

NAME
   env - run a program in a modified environment

SYNOPSIS
   env    [-]    [-i]    [-u   name]   [--ignore-environment]
   [--unset=name] [name=value]... [command [args...]]

DESCRIPTION
   This manual page documents the GNU version  of  env.   env
   runs  a  command with an environment modified as specified
   by the command line...

into this

\item[NAME] \hfill \\
   env - run a program in a modified environment

\item[SYNOPSIS] \hfill \\
   env    [-]    [-i]    [-u   name]   [-\hspace{.01cm}-ignore-environment]
   [-\hspace{.01cm}-unset=name] [name=value]... [command [args...]]

\item[DESCRIPTION] \hfill \\
   This manual page documents the GNU version  of  env.   env
   runs  a  command with an environment modified as specified
   by the command line...

I got the "\item[" part down but I can't get the other half for whatever reasons. The command I'm using:

 /[A-Z]/s/$/\] \\hfill \\\\/

works but shows up on lines I don't want.

Upvotes: 1

Views: 54

Answers (3)

NeronLeVelu
NeronLeVelu

Reputation: 10039

Try this, assuming header ar only line starting with a capital letter

sed '/^[A-Z]/ s/.*/\\item[&] \\hfill \\\\/' YourFile

for the \hspace{.01cm}, you shold define when it should occur (condition for)

Upvotes: 1

Kerwin
Kerwin

Reputation: 1212

sed -r 's/^([A-Z]+)$/\\item[\1] \\hfill \\\\/' file.txt

you can do it by one step.

Upvotes: 0

potong
potong

Reputation: 58400

This might work for you (GNU sed):

sed 's/^[[:upper:]]\+$/\\item[&] \\hfill \\\\/;s/\[--/[-\\hspace{.01cm}-/g' file

If you do not have GNU sed, try:

sed -e 's/^[[:upper:]][[:upper:]]*$/\\item[&] \\hfill \\\\/' -e 's/\[--/[-\\hspace{.01cm}-/g' file

Upvotes: 0

Related Questions