Stream editing Issue

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Stream editing Issue
# 1  
Old 11-23-2011
Stream editing Issue

here's my statement:
Code:
sed -i '4s/$/ #/' sed_test_file.txt

I want to edit the n'th line (the '4' is for the example) of the file (but you can't just put the n instead of the 4)

sed_test_file is a simple text file containig text only

I tried to use $urlCounter instead of 4, as urlCounter is a variable modified with every iteration. no success there

Ben Gurion University
Be'er Sheva
Israel
Dr. A. Goldfeld
Course Num 371.1.2473

Last edited by vbe; 11-24-2011 at 11:54 AM..
# 2  
Old 11-23-2011
For this simple sed programme, you could use double quotes and thus have your shell variable expand in the string passed to sed. You'll need to be careful and escape the dollar sign so that the shell doesn't try to expand it.

However, if you are going to end up making multiple passes across the same text file, there might be a more efficient way than lots of sed processes.

Example with double quotes and escapes:
Code:
line=4
sed -i "${line}s/\$/ #/" sed_test_file.txt

This User Gave Thanks to agama For This Post:
# 3  
Old 11-24-2011
Hi ganraveh,

I don't know whether i understand your requirement currectly or not.
you can make a shell script, something like this:
Code:
bash-2.03$ cat sd.sh
echo "enter line number: "
read n
sed ''$n's/$/ #/' r2.txt

when u run the script, it will ask for the line no.

regards,
This User Gave Thanks to forroughuse For This Post:
# 4  
Old 11-25-2011
re: agama

"However, if you are going to end up making multiple passes across the same text file, there might be a more efficient way than lots of sed processes.

Example with double quotes and escapes:
Code:
line=4
sed -i "${line}s/\$/ #/" sed_test_file.txt

[/QUOTE]

thanks for the help! it works as i wanted. but now i'm curious: how could it run more efficiently?
# 5  
Old 11-25-2011
Quote:
Originally Posted by ganraveh

thanks for the help! it works as i wanted. but now i'm curious: how could it run more efficiently?

Glad that it works. My efficiency comment was based on the assumption that you were invoking sed multiple times -- I could be wrong as you might not be doing this at all, it seemed to me from your request that you might. To make a more precise comment about it I (we) would need to see the relevant parts of the script.
This User Gave Thanks to agama For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert vi editing to text editing

Dear Guru's I'm using Putty and want to edit a file. I know we generally use vi editor to do it. As I'm not good in using vi editor, I want to convert the vi into something like text pad. Is there any option in Putty to do the same ? Thanks for your response. Srini (6 Replies)
Discussion started by: thummi9090
6 Replies

2. Solaris

Crontab editing issue

Hi, OS - SunOS I gave crontab -e then it returns zero. $ crontab -e 0 ? ? Crontab status - $ ps -ef | grep cron root 2481 1 0 May 12 ? 0:18 /usr/sbin/cron Please help (9 Replies)
Discussion started by: nag_sathi
9 Replies

3. Shell Programming and Scripting

[Video stream] network stream recording with mplayer

Hi I used this command: mplayer http://host/axis-cgi/mjpg/video.cgi -user root -passwd root \ -cache 1024 -fps 25.0 -nosound -vc ffh264 \ -demuxer 3 -dumpstream -dumpfile output.avi It's ok but... Video Playing is very fast! Why? Is it a synch problem? What parameter I have to use for... (1 Reply)
Discussion started by: takeo.kikuta
1 Replies

4. UNIX for Dummies Questions & Answers

Get the last value in the input stream

Hello Everyone, The situation is: $ alias rm="sh abc.sh" #abc.sh: read input echo "The input was: " $input $ rm xyz.txt should return "The input was: xyz.txt" Here, I want the alias "rm" to execute the file "abc.sh", where "abc.sh" should take "xyz.txt"... (1 Reply)
Discussion started by: qasim
1 Replies

5. Ubuntu

Stream video

Hello, I have Ubuntu 64 bit 9.04 install dual boot I can't see stream videos through Opera I couldnt see it in Firefox did this: "sudo apt-get install ubuntu-restricted-extras flashplugin-nonfree" Now I can see it in firefox but not in Opera Should not care and just use just Firefox. thanks (0 Replies)
Discussion started by: Pitroadrush
0 Replies

6. UNIX for Advanced & Expert Users

the stream edditor

:D i have searched through several sites on google for a match up for sed that would go indepth into the workings of this interactive edditor.. I use it every day and it is the edditor of my choice so i know a few tricks but would very much like to get into the more complex uses of sed.. all the... (2 Replies)
Discussion started by: moxxx68
2 Replies

7. Programming

File I/O Stream

Hi All, I am trying to read data from two files and then compare them and only print the records on the screen that have a same ID.i.e TAGNO =CUSTOMERNO For Eg My Input Files are (a) Transaction (b) Customer detail The data in file a is like: TagNo Date Time Station... (0 Replies)
Discussion started by: rooh
0 Replies

8. UNIX for Dummies Questions & Answers

Stream MP3s

What is a good and easy to set up program on Unix FreeBSD for playing streaming MP3s. Where can I get it and how do I install it? This is FreeBSD 4.3. Thanx. (2 Replies)
Discussion started by: ByondF1
2 Replies

9. Programming

stream output??

Hi all, I've a structure let's say typdef struct a { int a; int b; char* string} b; I need to make the function smth like readdir(), returning everytime it's called pointer to the next structure. Let's say functions would read the next file in the directory and all the permitions put it in... (2 Replies)
Discussion started by: solvman
2 Replies
Login or Register to Ask a Question