add a string to a file without line break


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers add a string to a file without line break
# 1  
Old 10-24-2012
add a string to a file without line break

I searched and found "echo -n" and "printf" are solution for this, but they are not
here:
Code:
[wadhwaso@nxnixd01 ~]$ echo "hello" >> test 
[wadhwaso@nxnixd01 ~]$ cat test 
hello 
[wadhwaso@nxnixd01 ~]$ echo -n "world" >> test 
[wadhwaso@nxnixd01 ~]$ cat test 
hello 
world 
[wadhwaso@nxnixd01 ~]$ echo -n " seriously?" >> test 
[wadhwaso@nxnixd01 ~]$ cat test 
hello 
world seriously?

This is not successful first time because that line already had a /n. this will be the case for every file.

I am not very comfortable with awk and sed, thats why I wanted to use these.
If I dont have any choice can someone tell me how to add "elevator=noop" in /etc/grub.conf via awk or sed ??

Last edited by stunn3r; 10-24-2012 at 06:07 PM.. Reason: formatting ..
# 2  
Old 10-24-2012
Code:
echo -e "hello\c" > file

Quote:
Originally Posted by stunn3r
If I dont have any choice can someone tell me how to add "elevator=noop" in /etc/grub.conf via awk or sed ??
Not sure why you're bothered about new line addition along with string in /etc/grub.conf.
You can simply do this: echo "elevator=noop" >> /etc/grub.conf
# 3  
Old 10-24-2012
See if this is what you want:
Code:
$ cat t
col1,col2,col3,col4,col5,col6,col7
a1,a2,a3,a4,a5,a6,a7
b1,b2,b3,b4,b5,b6,b7
c1,c2,c3,c4,c5,c6,c7
d1,d2,d3,d4,d5,d6,d7

** Add string ",string" to end of each line with sed
$ sed 's/$/,string/' t
col1,col2,col3,col4,col5,col6,col7,string
a1,a2,a3,a4,a5,a6,a7,string
b1,b2,b3,b4,b5,b6,b7,string
c1,c2,c3,c4,c5,c6,c7,string
d1,d2,d3,d4,d5,d6,d7,string

# 4  
Old 10-24-2012
Quote:
Originally Posted by balajesuri
You can simply do this: echo "elevator=noop" >> /etc/grub.conf
I doesn't matter if I place this parameter in new line ??

---------- Post updated at 03:00 AM ---------- Previous update was at 02:58 AM ----------

Quote:
Originally Posted by spacebar
See if this is what you want:
Code:
$ cat t
col1,col2,col3,col4,col5,col6,col7
a1,a2,a3,a4,a5,a6,a7
b1,b2,b3,b4,b5,b6,b7
c1,c2,c3,c4,c5,c6,c7
d1,d2,d3,d4,d5,d6,d7

** Add string ",string" to end of each line with sed
$ sed 's/$/,string/' t
col1,col2,col3,col4,col5,col6,col7,string
a1,a2,a3,a4,a5,a6,a7,string
b1,b2,b3,b4,b5,b6,b7,string
c1,c2,c3,c4,c5,c6,c7,string
d1,d2,d3,d4,d5,d6,d7,string

Thanks for that but I want it only at the end of last line, How can we achieve that ??
# 5  
Old 10-24-2012
To add to last line try:
Code:
sed '$s/$/,string/' infile

# 6  
Old 10-25-2012
One more with awk ...
Code:
 awk -F, '{print$0 ",string"}' test.txt

# 7  
Old 10-26-2012
Hi,

The command with argument " -i" is working fine on local system but not working when I try to put it in my script. Here:
Code:
#!/bin/bash

GRUB="/home/wadhwaso/test"
_pair="$(</home/wadhwaso/login.txt)"
Server_info="/home/wadhwaso/server_info"
for e in $_pair
do
             # extract user and ips for each $e in $_pair
        IFS='@'
        set -- $e
        _user="$1"
        _ip="$2"

        sleep 2
        echo "Connecting to server '${_ip}' via SSH..."
        ssh ${_user}@${_ip} "sudo /bin/sed -i '$s/$/ elevator=noop/' /home/wadhwaso/test "
        if [ $? -eq 0 ]; then
        echo "completed on ${_ip}" | tee -a "${Server_info}"
        else
        echo "not completed on ${_ip}" | tee -a "${Server_info}"
        fi
done

Can you see why this is happening. Also this is not working too:

Code:
ssh user@server  "sed -i '$s/$/ elevator=noop/' /home/wadhwaso/test"


Last edited by stunn3r; 10-26-2012 at 03:26 AM.. Reason: formatting
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace and add line in file with line in another file based on matching string

Hi, I want to achieve something similar to what described in another post: The difference is I want to add the line if the pattern is not found. File 1: A123, valueA, valueB B234, valueA, valueB C345, valueA, valueB D456, valueA, valueB E567, valueA, valueB F678, valueA, valueB ... (11 Replies)
Discussion started by: jyu3
11 Replies

2. Shell Programming and Scripting

Search a string in a text file and add another string at the end of line

Dear All I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB... (5 Replies)
Discussion started by: suryanarayana
5 Replies

3. Shell Programming and Scripting

Search a string in a text file and add another string at the particular position of a line

I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB and add/replace... (1 Reply)
Discussion started by: suryanarayana
1 Replies

4. Shell Programming and Scripting

Modify a file by another file: add new line and variable after string is found

hello, I have problem with writing/adjusting a shell script. I searched forum and unfortunately couldn't write scipt based on the information I found. I never wtire such so it's hard for me and I do need to modify one script immediately. case looks like: 1. 'file' that needs to be modified... (3 Replies)
Discussion started by: bipbip
3 Replies

5. Shell Programming and Scripting

Add Some String on each line of file

HI Guys, I am new user its my first post. I have one file in DIR XYZ. File name is ABCDEF.log Now i want to add that file in each line of file. I have data in file ABCDEF.log TestKLFH TEstLKHU HESTLJHG Now i want to update the file with below data ABCDEFG.log:TestKLFH... (5 Replies)
Discussion started by: asavaliya
5 Replies

6. Shell Programming and Scripting

break the string and print it in a new line after a specific word

Hi Gurus I am new to this forum.. I am using HP Unix OS. I have one single string in input file as shown below Abc123 | cde | fgh | ghik| lmno | Abc456 |one |two |three | four | Abc789 | five | Six | seven | eight | Abc098 | ........ I want to achive the result in a output file as shown... (3 Replies)
Discussion started by: kannansr621
3 Replies

7. Shell Programming and Scripting

Add line break for each line in a file

I cannot seem to get this to work.. I have a file which has about 100 lines, and there is no end of line (line break \n) at the end of each line, and this is causing problem when i paste them into an application. the file looks like this this is a test that is a test balblblablblhblbha... (1 Reply)
Discussion started by: fedora
1 Replies

8. Shell Programming and Scripting

line break if string exceeds 100chars

I am required to develop a script to look for specific strings in the /var/adm/messages file on our Solaris10 host. When an entry is found in the file then I need that line to be piped to a log file. I have a script with the criteria i just need help with the manipulation of the text string. 1.... (6 Replies)
Discussion started by: s1ckle
6 Replies

9. Shell Programming and Scripting

Trim whitespace and add line break

All, I'm a newbie at shell scripting and regular expressions and I just need to take a file that's arranged like the one below, remove all leading and trailing whitespace and add a line break after each word. I've been able to remove a few spaces using various awk, sed and Perl scripts, but... (7 Replies)
Discussion started by: moose1
7 Replies

10. Shell Programming and Scripting

how to insert line break + string in vi (search & replace )

Hello all i have big test file that has allot of structure text something like this : <foo1 *.html> <blah action> somthing 1 somthing 2 </blah> </foo1 > now i will like to insert 2 more lines of text below the <blah action> so it will be like : <foo1... (1 Reply)
Discussion started by: umen
1 Replies
Login or Register to Ask a Question