Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

alternative for "sed -i" (and without temp file)

Status
Not open for further replies.

w5000

Technical User
Nov 24, 2010
223
0
0
PL

hello,
case: sed on a system doesn't support -i

I need to remove leading hashe(s) from matched patern lines in a file.
I can sed + temp file but how could I skip temp file and do change it directly in text file?

Code:
$ echo "        ############# 456\n\t ## 12345 \t\n     2345     # ## ####12345#\t   aa\n%### \t12345 bb\n11 22 33 44 55"
        ############# 456
         ## 12345
     2345     # ## ####12345#      aa
%###    12345 bb
11 22 33 44 55
$ D=12345
$ echo "        ############# 456\n\t ## 12345 \t\n     2345     # ## ####12345#\t   aa\n%### \t12345 bb\n11 22 33 44 55"|sed "/${D}/ s/^[[:space:]]*//;/${D}/ s/[[:space:]]*$//;/${D}/ s/[[:space:]][[:space:]]*/\ /g;/${D}/ s/^#[# ]*//"
        ############# 456
12345
2345 # ## ####12345# aa
%### 12345 bb
11 22 33 44 55
$
 
Hi

The usual answer is, use Perl ( or Ruby ) :
Code:
perl -ipe 's/^#+//' /input/file

[gray]# or[/gray]

ruby -ipe 'sub /^#+/,""' /input/file

Or if the file is small, use a variable instead of temporary file :
Code:
content="$( sed 's/^#[# ]*//' /input/file )"
echo "$content" > /input/file

w5000 said:
how could I skip temp file and do change it directly in text file?
That is really difficult. Not even [tt]sed -i[/tt] does such thing. ( It writes the result to a new file and renames it after. )

( Note that I not reproduced the entire Sed code to to keep the examples simple and readable. )

Feherke.
feherke.github.io
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top