UNIX commands question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting UNIX commands question
# 1  
Old 03-19-2015
UNIX commands question

I am using HP-UXsystem. I am unable to use the below sed commands. could you please let us know the alternate commands for it.

Code:
>sed '/unix/ c "Change line"' file.txt
 
sed '/unix/ i "Add a new line"' file.txt

any inputs please
# 2  
Old 03-19-2015
Hello ramkumar15,

Assuming that your requirement is to search for a text and then substitute it with a new text and edit the input file too, if this is the exact requirement then following may help.
Code:
sed -i '/unix/s/Add a new line/' file.txt

Also if you want to change the text for all matches then following will help.
Code:
sed -i '/unix/s/Add a new line/g/' file.txt

Thanks,
R. Singh
# 3  
Old 03-19-2015
Code:
Add a line before a match
 
sed -i '/unix/s/Add a new line/' file.txt

Code:
 
to Change a line
 
sed '/unix/ c "Change line"' file.txt

# 4  
Old 03-19-2015
Some seds accept above syntax, others don't. man sed:
Quote:
i \
text Insert text, which has each embedded newline preceded by a backslash.
c \
text Replace the selected lines with text, which has each embedded newline preceded by a backslash.
So - you might want to try this syntax with the \ .
# 5  
Old 03-19-2015
On HPUX systems sed does not have -i switch.

For 'instant' substitution you will have to use perl with -s -i switches, ed or a sed with temporary file.
Code:
sed "s/string/newstring/g" inputfile > inputfile_tmp && mv inputfile_tmp inputfile

# 6  
Old 03-19-2015
i and a and c commands need multi-line (not possible in t/csh!), where the previous line ends with a \ character:
Code:
sed '/unix/ i\
Insert a new line
' file.txt

Code:
sed '/unix/ a\
Append a new line
' file.txt

Code:
sed '/unix/ c\
Change to line1\
and line2
' file.txt

They can as well be done by the s (substitute) command.
where the c (change) with a one-line replacement can be done in one line:
Code:
sed '/unix/ s/.*/Change line/' file.txt

To write the changes back to the input file, it is most safe to overwrite it (retaining the inode/owner/permissions):
Code:
sed '...' file.txt > file.txt.new && cp file.txt.new file.txt && rm file.txt.new

or
Code:
cp file.txt file.txt.old && sed '...' file.txt.old > file.txt && rm file.txt.old

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

UNIX commands question

if ] then rm -rf Can anyone tell me what are they checking in the if loop Please use code tags next time for your code and data. Thanks (2 Replies)
Discussion started by: ramkumar15
2 Replies

2. UNIX for Dummies Questions & Answers

Beginner UNIX question. tail and find commands

hey guys, i'm in a unix course.. and while this is a homework question - i did put alittle effort into it. just wanted to ask before trial and error drives me nuts. question 13 has us saving the last 30 characters of a file into another file and question 14 has us saving the list of all the... (1 Reply)
Discussion started by: labelthief
1 Replies

3. UNIX for Dummies Questions & Answers

Noob scripting question with android ADB commands

Hi I'm pretty new to scripting and I've been googling around looking for an answer but have yet to come up with a proper solution. I work with multiple android devices at a time and I'm looking to simplify my life with a script. Basically I'm looking for a script that takes the device ID's and then... (2 Replies)
Discussion started by: Onyoursix
2 Replies

4. Homework & Coursework Questions

Unix commands Help

I am struggling with one of my assignment questions, not sure how to go about coding it question is 2. The fille ~eliwp/marks is a colon separated list of students and their marks. Write a script to process this file printing out the id's of the students that have failed the module.... (2 Replies)
Discussion started by: tina_2010
2 Replies

5. Shell Programming and Scripting

question about shell commands...

Hello, I have the following is a txt file: !!! man.uni.com address 141.000.001 to you.uni.com !!! man.uni.com address 141.000.002 to wae1.uni.com !!! man.uni.com address 141.000.003 to kim2.uni.com I want to get you,wae and kim from the text and put them in a diffrent file in the following... (2 Replies)
Discussion started by: andrew1400
2 Replies

6. Shell Programming and Scripting

Question about awk commands

what does this mean? cat /etc/passwd | awk -F: '{print $5}' | \ awk -F, '{print $1}' | \ awk '{print tolower($l)};{print tolower($2)}' | \ grep -v '^$' >> local_tmp (1 Reply)
Discussion started by: hin-linux
1 Replies

7. Shell Programming and Scripting

ssh commands in ksh question

Is there a way to shorten these commands? because this script asks for a password 3 times scp -p /usr/local/bin/${script_name} ${servername$iy]}://usr/local/bin/ ssh ${servernames} /usr/local/bin/${script_name} ssh ${servernames} rm -f /usr/local/bin/${script_name} Basically, I'm creating a... (3 Replies)
Discussion started by: pdtak
3 Replies

8. UNIX for Dummies Questions & Answers

Running UNIX commands remotely in Windows box from Unix box – avoid entering password

I am able to run the UNIX commands in a Windows box from a UNIX box through "SSH" functionality. But whenever the SSH connection is established between UNIX and Windows, password for windows box is being asked. Is there a way to avoid asking password whenever the SSH connection is made? Can I... (1 Reply)
Discussion started by: D.kalpana
1 Replies

9. UNIX for Dummies Questions & Answers

Question about batched commands

Hi everyone I am a newbbie in the linux enviroment. My problem is: I have tried to use the at command to compile and keep running a program and close the terminal window. Every time I use the at command I get the next error "Can't open /var/run/atd.pid to signal atd. No atd running?" ... (2 Replies)
Discussion started by: fenixbeta
2 Replies

10. UNIX for Dummies Questions & Answers

Maingrame to UNIX sending UNIX commands

I want to know if there is a way to send unix commands thru FTP from a mainframe to kick off Autosys Jobs. I just need to send a command from the mainframe to UNIX and have UNIX execute that command. (2 Replies)
Discussion started by: skammer
2 Replies
Login or Register to Ask a Question