r/shell • u/rcentros • Dec 08 '24
Changing a sed variable in a shell script
Hi,
I'm trying to set up a shell script that removes a specific number of prepended spaces at the beginnings of lines. The following shell script works to do this...
#!/bin/bash
clear
read -p 'file: ' uservar
sed -i 's/\(.\{4\}\)//' $uservar".txt"...but I don't always have files with 4 prepended spaces.
I would like to add another input variable ("spaces") to change the 4 to whatever number I input.
As you can guess, I'm not really a programmer, the sed line (above) was found on another site and incorporated into this extremely simple shell script. I can edit the shell script with each use, but I would prefer to add the extra input variable, mostly so I can pass this shell script on to others who might need it.
Thanks for any pointers.
EDIT: I figured it out (well, found out how to do it, anyhow). For my number entry I needed to add an -r and a -p for a number entry (I have no idea why). Once I did that I finally read that I needed single quotes to separate the variable from the rest of the sed command in my line. I don't completely understand it, but it works.
For what it's worth, here it is...
#!/bin/bash
clear
read -p 'file: ' uservar
read -r -p 'spaces: ' number
sed -i 's/\(.\{'$number'\}\)//' $uservar".txt"
2
u/BetterScripts Dec 11 '24
Just wanting to add to what has already been said, and answer some of your other questions.
Since you seem quite confused, but eager to learn, I've gone into a bit of detail here, sorry for the length and if it's at all condescending!
Firstly, you can use the
sedcommand:Which works as follows:
-isedto work "in-place" and use$FileNameas both input and output"s/^ \{$NumberOfSpaces\}//"seduses this value the shell will process this and replace$NumberOfSpaceswith any value it contains (e.g. it becomes"s/^ \{4\}//")sedthen operates on$FileName, and for each line of input performs a substitution (hences)sedsubstitutions take the forms/<MATCH>/<REPLACE>/where<MATCH>is a regular expression which is used for each line and<REPLACE>is the value used to replace any matches. In this case:<MATCH>is^ \{4\}which matches EXACTLY 4(i.e. space) characters but only at the start of a line (this is what the^means)<REPLACE>is empty, so any match is deleted.Note that
-iis a non-standard extension tosed, if you need to use the command on other machines it may not be available. In this case you need to use a temporary file, e.g.:Since you seem to now be using a backup file anyway, then it would be better just to skip the
-i. This also makes it easier to detect unchanged files:Which, as you correctly suspected, is otherwise not really possible in any sane way with
sed(awkwould be better for that, but is a lot more complex).Some other comments on your code:
\(and\)in yoursedcommand, although they are harmless - these are used to group expressions and to "capture" parts of matches - neither is required here.s/\(.\{$2\}\)//will delete a number of characters from every line even if they are not spaces - the.character is a special character that matches ANY single character. This is not what you want!'and"can be used for any quote but'tells the shell the value should be used as is while"tells the shell to replace variables (like$2) in the value before doing anything else. Except in specific circumstances you should always use shell variables inside"quotes or eventually things will break. Specifically:$uservar".txt"/$1".txt"should be written as"$uservar.txt"/"$1.txt"- although both will produce the same results in many situations, if$uservar/$1contain, for example, any spaces the version you have will result in errors's/\(.\{'$2'\}\)//'is better written as"s/\(.\{$2\}\)//"To answer some of your other questions:
-Eargument forsedtells it to use a different type of regular expressions for<MATCH>known as Extended Regular Expressions (ERE), the default being known as Basic Regular Expressions (BRE). ERE and BRE are similar, but use different formatting. Often ERE can be easier to read. If using-Ethen\{$NumberOfSpaces\}is instead written{$NumberOfSpaces}. Note that many versions ofseddo not support-E, although it is now required by the standard.#!/usr/bin/env bashinstead of#!/bin/bashis generally preferred for a number of reasons that are relatively technical, but it makes the script more portable. You can read more is this answers to this question: "why do bash scripts start with #!"[[ -z $1 ]]is exactly the same astest -z $1and tests if$1is empty, i.e. "zero" or not, there is alsotest -n $1which tests for the oppositeHopefully that helps you understand more of what's going on and what you're doing.
1
u/cdrt Dec 09 '24 edited Dec 09 '24
If you don’t mind, I have some constructive criticism for your script
read -rrather thanreadso that the shell will read the input as is an you will have fewer surprisessed -Eso you can use the nicer regex syntax and avoid having so many backslashes.because that will gobble up everythingWith this, I would rewrite your script like this:
Then, assuming you named your script “delete_spaces”, you can run it like
delete_spaces file_nameto just delete four spaces or likedelete_spaces file_name 6to delete 6 spaces