Noobie
Noobie

Reputation: 55

Replace all spaces in a certain part UNIX

Hi all I'm trying to replace all spaces beginning in certain part of my file. I tried to do it but I can't make it to start in a certain part.

i tried this sed "s/\s/_/g" < file.txt > file_1.txt but all of the spaces turn into underscore.

inside file.txt :

My Name
Favorite Food
Favorite Color
Time is gold
List of Dogs:
Shi ba Inu
Sibe rian Husky
Labra dor Retriever
Ger man Shep herd
Bull Doge
Be agle
chi hua hua
Bull Ter rier

expected file_1.txt:

My Name
Favorite Food
Favorite Color
Time is gold
List of Dogs:
Shi_ba_I_nu
Sibe_rian_Husky
Labra_dor_Retriever
Ger_man_Shep_herd
Bull_Doge
Be_agle
chi_hua_hua
Bull_Ter_rier

Upvotes: 0

Views: 67

Answers (5)

Ed Morton
Ed Morton

Reputation: 204628

keep it simple, obvious, robust, portable, etc. and just use awk:

$ awk 'found{gsub(/[[:space:]]/,"_")} /:/{found=1} {print}' file
My Name
Favorite Food
Favorite Color
Time is gold
List of Dogs:
Shi_ba_Inu
Sibe_rian_Husky
Labra_dor_Retriever
Ger_man_Shep_herd
Bull_Doge
Be_agle
chi_hua_hua
Bull_Ter_rier

Upvotes: 0

clarity123
clarity123

Reputation: 2046

Given your initial input file.txt:

My Name
Favorite Food
Favorite Color
Time is gold
List of Dogs:
Shi ba Inu
Sibe rian Husky
Labra dor Retriever
Ger man Shep herd
Bull Doge
Be agle
chi hua hua
Bull Ter rier

You can try this:

$ sed '/List of Dogs/,$s/\s/_/g;s/List_of_Dogs/List of Dogs/g' file.txt

Which results:

My Name
Favorite Food
Favorite Color
Time is gold
List of Dogs:
Shi_ba_Inu
Sibe_rian_Husky
Labra_dor_Retriever
Ger_man_Shep_herd
Bull_Doge
Be_agle
chi_hua_hua
Bull_Ter_rier

Explanation

  • sed commands can be split by ;
  • first part starts with getting an address, which is the form range start,range end. Finds the line that List of Dogs starts at. And $ specifies last line of file, for the range end part of this syntax
  • so just for this address range, your search and replace command is done: $s/\s/_/g
  • but unfortunately the command also replaced and resulted in List_of_Dogs: so second command s/List_of_Dogs/List of Dogs/g is just a workaround to convert it back

Upvotes: 1

Lars Fischer
Lars Fischer

Reputation: 10229

If you want the substitution happen only after the :, use something like this:

sed -r  '/:/,$ s/\s/_/g;' file.txt > file_1.txt

The substitution is restricted from a line containing : until the end of the file $.

Upvotes: 1

tripleee
tripleee

Reputation: 189936

If you want the substitution to happen only after "List of Dogs", try

sed -e '1,/List of Dogs:/b' -e 's/\s/_/g'

The command b means "branch" (to the end of the script, i.e. bypass the substitution) and the address range specifies this action for the first line through the first line matching the regex.

Upvotes: 1

user4278933
user4278933

Reputation:

You have the answer and you don't know it =)

You say you want to replace the spaces, but you have not said what you want to replace them with. I suspect, you want to replace them with a no-space character, right?

sed "s/ //g" $original_file > $new_file

or referencing the space with \s the following should also work

sed "s/\s//g" $original_file > $new_file

The syntax is basically

sed "s/find_this/replace_with/g" $original_file > $new_file

I hope that helps...

Upvotes: 0

Related Questions