replace gnu sed command ~ with sed command

replace gnu sed command ~ with sed command

Post by Dirk Joo » Thu, 02 Feb 2006 19:43:14



Hi,

I've written a little sed script which is working in my environment. But
  I tried it in another environment and there it didn't work because the
~ command was unknown there.

Here the script:
# beginning at line 3, remove every 8th line
# beginning at line 4, remove every 8th line
# beginning at line 5, remove every 8th line
# beginning at line 6, remove every 8th line
sed -e '3~8d
         4~8d
         5~8d
         6~8d' file1.txt > file2.txt

How could I replace this gnu sed command with a sed command which is
valid in all environments?

I tried different things, but they didn't work.

For example I tried to replace this command as follows:

sed -e '3,${d;n;n;n;n;n;n;n}
         4,${d;n;n;n;n;n;n;n}
         5,${d;n;n;n;n;n;n;n}
         6,${d;n;n;n;n;n;n;n}' file1.txt > file2.txt

What am I doing wrong?

I'm new to sed, but I really like it and want to learn more about it and
I'm very interested in your answers.

Thanks for your help

Dirk

 
 
 

replace gnu sed command ~ with sed command

Post by Stephane Chazela » Thu, 02 Feb 2006 20:00:26



> Hi,

> I've written a little sed script which is working in my environment. But
>   I tried it in another environment and there it didn't work because the
> ~ command was unknown there.

> Here the script:
> # beginning at line 3, remove every 8th line
> # beginning at line 4, remove every 8th line
> # beginning at line 5, remove every 8th line
> # beginning at line 6, remove every 8th line
> sed -e '3~8d
>          4~8d
>          5~8d
>          6~8d' file1.txt > file2.txt

sed '1!n;1!n;n;n;N;N;N;d'

or:

awk '(NR+5) % 8 > 3'

--
Stephane

 
 
 

replace gnu sed command ~ with sed command

Post by Dirk Joo » Thu, 02 Feb 2006 23:41:32


Stephane Chazelas schrieb:


>>Hi,

>>I've written a little sed script which is working in my environment. But
>>  I tried it in another environment and there it didn't work because the
>>~ command was unknown there.

>>Here the script:
>># beginning at line 3, remove every 8th line
>># beginning at line 4, remove every 8th line
>># beginning at line 5, remove every 8th line
>># beginning at line 6, remove every 8th line
>>sed -e '3~8d
>>         4~8d
>>         5~8d
>>         6~8d' file1.txt > file2.txt

> sed '1!n;1!n;n;n;N;N;N;d'

> or:

> awk '(NR+5) % 8 > 3'

Thank you very much. I only tested your sed answer because until now I
never used awk. But your sed answer is working great.
 
 
 

replace gnu sed command ~ with sed command

Post by Stephane Chazela » Fri, 03 Feb 2006 01:03:43


[...]

Quote:>>>sed -e '3~8d
>>>         4~8d
>>>         5~8d
>>>         6~8d' file1.txt > file2.txt

>> sed '1!n;1!n;n;n;N;N;N;d'

Or, more lebibly:

sed -n 'p;n;p;n;n;n;n;n;p;n;p'

--
Stephane

 
 
 

1. String manipulation error & how to use sed in a sed command?

Hello all,

Let me explain my situation, am writing script in Bourne shell.The
portion of script follows
======
for val in 1 2 3 4 5
do
k$val=`sed -n "$val"p myfile.txt`    ##assume myfile.txt containts 5
lines
done
echo $k3  #---Just to check.
=======

I just want to assign the first line from myfile.txt(one word ony) to
k1 variable ....like k2 ..k3...upto...k5 vairable Its not
working...intead of that when i use

for val in 1 2 3 4 5
do
expr k$val=`sed -n "$val"p myfile.txt"
done
echo $k3

its just echoed what am expecting but the echo $k3 line replied null
value.

When i do it from outside (in cmd line) am getting the exact one,
instead of creating 20 variables (my file contains 20 lines with
single word in each line) i want to use this method so that in the
later part i can use the $k1 in the sed command to replace the values.
I used with single quotes , back quotes & double quotes all but no use

Anybody have any idea how to overcome this?.

Ok my second question is on sed...that is the extension part of this
program

it is possible to use sed command in the sed .For example

I want to replace a text with a first line from another file.

#sed -e 's/the-text-to-be-replace/`sed -n 1p myfile.txt`/g' < file1 >
file2

Its just places the entire command instead of first line from the
file.

Any advice would be muchly appreciated.If you have any options to
solve this  in awk kindly let me know.

Thanks & Regards

2. I have alot of Problems

3. sed command to replace text with blank spaces

4. how to install

5. Q: How to replace TABS with SED-command?

6. Keyboard & Yggdrasil Probl

7. How to use Sed command for Search and Replace more than one word in a file

8. Help, backup the system

9. Sed command to replace text in several files

10. insert file in sed replace command

11. sed sed sed

12. . 2 sed FAQs, sed exes, sed cetras; URLs

13. SED SED SED...