0

I would like to replace a specific line in a text document using a configurable argument. Text document example:

DialogUpdateTags,
DialogProductNotFound
{
    width:                          1000;
    height:                         166;
}

In the example above I want to edit line 4 "1000" specifically so the script doesn't edit other width values that might also be 1000. So far I have this:

echo "Enter the desired width size in pixels"
read pixelsize
echo "Width size will be $pixelsize"

After that I need a sed command, can anyone help? Is there a way to point SEP only to line 4 and not edit anything else?

D-One
  • 3
  • 2
  • You mean like `sed "4s/[^ ]*;/$pixelsize;/" filename`? – Beta May 16 '18 at 01:07
  • Or `sed -i 's/^\(\s*width:\s*\)[0-9][0-9]*;$/\1'"$pixelsize"';/' file`? or you can just double-quote, `sed -i "s/^\(\s*width:\s*\)[0-9][0-9]*;$/\1$pixelsize;/" file` This will work fine as long as there are no other `" width: number;"` lines. – David C. Rankin May 16 '18 at 01:19
  • 1
    Sorry, this is not the way StackOverflow works. Questions of the form "I want to do X, please give me tips and/or sample code" are considered off-topic. Please visit the [help] and read [ask], and especially read [Why is “Can someone help me?” not an actual question?](http://meta.stackoverflow.com/q/284236) – kvantour May 16 '18 at 10:23
  • On top of that, a simple search pops up the following posts : [Unix&Linux : Problem with passing a variable to sed](https://unix.stackexchange.com/q/330243/273492), [AskUbuntuu : How do I use variables in a sed command?](https://askubuntu.com/q/76808) and [Stack Overflow : shell variables in sed script](https://stackoverflow.com/q/7006910/8344060) – kvantour May 16 '18 at 10:28
  • Is that line unique, or is it possible other lines of the file will also match `width:\s+1000;`? If the line is distinct it's easy enough, but if you you'll have to contextualize it, which means something like loops and state variables. – Paul Hodges May 16 '18 at 13:58
  • Hi Paul, thanks for the reply. The line is not unique. There's no way to restrict sep to only Line 4? – D-One May 16 '18 at 21:22
  • @kvantour Sorry for my stupidity, I tried reading articles before but without any prior experience, the sheer amount of information is quite overwhelming. I am not even at the point where parsing the variable is the issue, I am still stuck on how to restrict SEP to a single specific line number on the file. – D-One May 16 '18 at 21:26
  • @D-One Don't take offence of my comments. I fully agree with you that every beginning is hard and often overwhelming. Furthermore, finding the right resources or coming up with the best search keywords is a daunting task. The tag-info on SO has generally good information for beginners which could help you out. (Eg. [tag:sed] or [tag:awk]) On top of that, I strongly support your quest for knowledge and encourage you to keep asking questions! – kvantour May 17 '18 at 12:56

2 Answers2

2

With GNU sed:

pixelsize="30"
sed -E "4s/(width: *)[^;]*/\1$pixelsize/" file

Output:

DialogUpdateTags,
DialogProductNotFound
{
    width:                          30;
    height:                         166;
}

If you want to edit your file "in place" use sed's option -i.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • Perfect, thank you so much Mr Cyrus! The number before the s/ is what points to the line number! Got it. – D-One May 17 '18 at 18:56
1
width="30"
path=textfile.txt
sed -i "4s/1000;/$width;/" "$path"
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
akash
  • 779
  • 5
  • 16