[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SED Download and Edit

Posted on 2006-05-11
13
Medium Priority
?
363 Views
Last Modified: 2013-12-26
I have a very large UNIX file (>4G) that I need to edit.  Every occurance of a tilde (~) not followed by a pipe (|) should be changed to a tilde followed by a space.  I am told that SED is a UNIX stream editing program that would be ideal for this kind of edit, and I am told SED is freely available on the web.

First, where can I get an SED download?

Second, what would the replace command line parameter look like to accomplish the task?
0
Comment
Question by:dlmedici
  • 3
  • 3
  • 2
  • +3
11 Comments
 
LVL 65

Accepted Solution

by:
rockiroads earned 200 total points
ID: 16658630
I take it u want sed for windows, as its free on Unix anyway
u can find a copy here
http://gnuwin32.sourceforge.net/packages/sed.htm

using sed is like substituing in vi

e.g.

sed 's/abc/def/'

changes abc to def

0
 
LVL 65

Expert Comment

by:rockiroads
ID: 16658659
in Unix, u display a file using cat <file>
In windows, its type <file>

Ive not tried sed for windows but in Unix to change a word, u do this

cat myfile.txt | sed s/OLDSTR/NEWSTR/

this will redirect to screen, so to redirect to new file

cat myfile.txt | sed s/OLDSTR/NEWSTR/ > newfile.txt

can u use pipes in DOS?

0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 16659191
Hi dlmedici,

> First, where can I get an SED download?
You forgot to mention the platform ... sed is available for most platforms .. a simple google search should provide you with a link
 
> Second, what would the replace command line parameter look like to
> accomplish the task?
sed 's:~|:~ :g' input_file_name > ouput_file_name

mv output_file_name input_file_name ----> this would replace your input file with the processed version ... Note that your original file will be lost by this command ... Avoid it if you are not sure.

>I have a very large UNIX file (>4G) that I need to edit.
sed might break on being given voluminous input ... You can still give it a try ... Following command might speed up the processing a bit for you ...

sed '/~|/ s/~|/~ /g' input_file_name > ouput_file_name

Cheers!
Sunnycoder
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 3

Assisted Solution

by:root_start
root_start earned 200 total points
ID: 16662710
Hi dlmedici,

Sunnycoder, your command is not doing what dlmedici need because you are changing every "~|" by "~ " and what is needed here, is: if "~" is NOT followed by a pipe, change this to "~ ".

You can use sed to do what you want, and if you want to change it in the file without redirecting the output, you can use the following: "sed -i" but if you want to change and redirect the output, just use the sed below:
==================================================================
sed 's/~/~ /g;s/~ |/~|/g' your_file > result.txt
==================================================================

I created the following file to check what you need:
==================================================================
[utest@Test]:# cat txt
~|aaaaaaaaaaaaaaaaaaaaaaaaaaaa~aaaaaaaaaaaaaaaaaa~|aaaaaaaaaaaaaaaaaaaaaaa
==================================================================

==================================================================
[utest@Test]:# sed 's/~/~ /g;s/~ |/~|/g' txt
~|aaaaaaaaaaaaaaaaaaaaaaaaaaaa~ aaaaaaaaaaaaaaaaaa~|aaaaaaaaaaaaaaaaaaaaaaa
==================================================================

As you can see, it did what you were looking for.

Hope it helps. =0)
0
 
LVL 3

Expert Comment

by:root_start
ID: 16662811
Ops... I forgot to comment that you can download SED from the following links:
==================================================================
GNU(Unix): http://www.gnu.org/software/sed/sed.html
Windows: http://gnuwin32.sourceforge.net/packages/sed.htm
==================================================================

You can download the binaries or you compile it by yourself if you don't find the specific compiled binaries for your plataform.

Again, I hope it helps. =0)
0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 200 total points
ID: 16663138
rockiroads.

No need for the UUOC.

sed s/OLDSTR/NEWSTR/ myfile.txt

is the same as

cat myfile.txt | sed s/OLDSTR/NEWSTR/

but doesn't need to fork an additional process.
0
 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 200 total points
ID: 16665052
Hi  root_start ,

Thanks for bringing the "not" to my attention. In that case, using two sed expressions would still be unnecessary ...:)

Hi dlmedici,

sed 's:~[^|]:~ :g'   infile > outfile

would be sufficient .... Since input data is voluminous, this would have significant impact on procesing time ....

Cheers!
sunnycoder
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 16665088
Hi dlmedici,

Another issue ...
>Every occurance of a tilde (~) not followed by a pipe (|) should be changed to a tilde followed by a space.
Do you want the next non-pipe character to be replaced with the space or would you like it to be still present with space added between ~ and this character?

If you wish to have it replaced, then above script would work fine .. if you wish to preserve it, then use

sed 's:~\([^|]\):~ \1:g'   infile > outfile

This would still not be able to handle two or more consecutive tildes... such input would necessiate use of multiple sed expressions and you might as well use root_start's solution in that case.

Cheers!
sunnycoder
0
 
LVL 3

Expert Comment

by:root_start
ID: 16666368
Hi dlmedici,

Since the input is too large, you can execute SED with nohup and let it executing in background. =0)
0
 
LVL 51

Assisted Solution

by:ahoffmann
ahoffmann earned 200 total points
ID: 16677293
most sed would fail with such huge files, I'd use perl instead

perl  -pe 's/~(?!\|)/~ /g' file
# or
perl -i.bak -pe 's/~(?!\|)/~ /g' file
0
 

Author Comment

by:dlmedici
ID: 16678360
Thank you, Everyone.  I'll forward these excellent suggestions to the fellow in charge of the file.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question