INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

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

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

(OP)

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
$ 

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

Hi

The usual answer is, use Perl ( or Ruby ) :

CODE --> command-line

perl -ipe 's/^#+//' /input/file

# or

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

Or if the file is small, use a variable instead of temporary file :

CODE --> command-line

content="$( sed 's/^#[# ]*//' /input/file )"
echo "$content" > /input/file 

Quote (w5000)

how could I skip temp file and do change it directly in text file?
That is really difficult. Not even sed -i 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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close