Check if line has a space


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check if line has a space
# 1  
Old 04-18-2012
Check if line has a space

Hi,

I want to check if the given line from a text file has a spaces in between. if it does, then I want to add '"' double quotes at the beginning and end of the line. Otherwise leave the line as it is.

For example, below is the sample content from my file.

Code:
$cat file.txt

test1
test2
test1 test2
test2 test3 test4

Now I want to change this output to
Code:
test1
test2
"test1 test2"
"test2 test3 test4"

I was able to do this using normal shell assignments. But I want to do it using sed and I am new to sed. please help.

Thanks,
# 2  
Old 04-18-2012
Hi,

try this:
Code:
cat YOURFILE.txt| awk -F " " '/ / {print "\"" $0 "\""};!/ / {print $0}'

# 3  
Old 04-18-2012
with sed:

Code:
sed 's/.* .*/"&"/g'

# 4  
Old 04-18-2012
Hi,

Thanks for the quick turnaround. I tried both the options. But they add double quotes for all lines.

What I want is to add double quotes only when the line has spaces in between.

As shown below, I want to skip the lines where there are no spaces in between.


Code:
test1
test2
"test1 test2"
"test2 test3 test4"

# 5  
Old 04-18-2012
have a space after the word?
eventually use
Code:
sed 's/ *$//g;s/.* .*/"&"/g'

# 6  
Old 04-18-2012
Hi Pokerino,

I want add double quotes only when the line containing words have spaces in between and leave the other lines as they are.

Code:
test1
test2
"test1 test2"
"test2 test3 test4"

Thanks,
# 7  
Old 04-18-2012
It worked here:

Code:
[mute@geek ~/svajhala]$ cat test.txt

test1
test2
test1 test2
test2 test3 test4
[mute@geek ~/svajhala]$ sed 's/.* .*/"&"/g' test.txt

test1
test2
"test1 test2"
"test2 test3 test4"
[mute@geek ~/svajhala]$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check the mount space

Hi, When i check the cifs = cifs i could get two dupliates of cifs present in unix. so the system couldnt check. could you please let me know how to make the change code to find out the unique one in the line 6. 1NTBD="/ft/sv/ss" 2check_ntfs() 3{ 4 # Check that the necessary NT areas are... (1 Reply)
Discussion started by: keerthi2016
1 Replies

2. UNIX for Dummies Questions & Answers

How to remove fields space and append next line to previous line.?

awk 'BEGIN{FS = "Ç"} NR == 1 {p = $0; next} NF > 1 {print p; p = $0} NF <= 1 {p = (p " " $0)} END {print p}' input.txt > output.txt This is what the input data file looks like with broken lines Code: 29863 Ç890000000 Ç543209911 ÇCHNGOHG Ç000000001 Ç055 ... (4 Replies)
Discussion started by: cumeh1624
4 Replies

3. Shell Programming and Scripting

Check disk space

I am trying a script which will alert if disk space crosses some threshold, i googled it and got some scripts already, but they are not working with my server. The problem is, my filesystem names are big, so the sizes are moving to the second line. just like below any ideas? thanks in advance... (8 Replies)
Discussion started by: karthikeayan
8 Replies

4. Homework & Coursework Questions

Looking to check disk space

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Need to check the disk space and if any portion disk space usage high then write to one file, later will... (5 Replies)
Discussion started by: Jasminshakoor
5 Replies

5. Shell Programming and Scripting

how to check disk space

Hi All, pls go thru the below code and help me. when i check "df -k" in my solaris system .. it will show like below.. fd 0 0 0 0% /dev/fd /dev/dsk/c0t0d0s3 20171281 2319266 17650303 12% /var /dev/dsk/c0t0d0s4 10085260 443854... (15 Replies)
Discussion started by: steve2216
15 Replies

6. Shell Programming and Scripting

Stripping out more than a space from a line, but keep single space.

Hi all, Is there a way to perform the above, I am trying to strip out more than one space from a line, but keep the single space. See below output example. My Name is test test2 test3 test4 test5 My Name is test test2 test3 test4 test5 Please note that the lines would contain... (7 Replies)
Discussion started by: eo29
7 Replies

7. Shell Programming and Scripting

How to print the words in the same line with space or to the predefined line?

HI, cat test abc echo "def" >> test output is cat test abc def the needed output is cat test abc def and so on (5 Replies)
Discussion started by: jobycxa
5 Replies

8. Solaris

Disk space check

Hi, I have a question regarding finding free space on the disk of a solaris machine. Many mount points are available in my machine. Right now i am using df -b option to get the free disk space available. I have an assignment to check free space on the disk. I pass the directory as a... (6 Replies)
Discussion started by: raghu.amilineni
6 Replies

9. Shell Programming and Scripting

check space !!!

The have written the below script :- ============================ SPACE=`bdf /DATA_TRANSFER|awk '{print $4}' |grep "%"` TEST="96%" if then echo "Continue ....." sleep 2 else echo " Current space for DATA_TRANSFER is less than 02 %" echo " Pls clear space and than continue ....."... (2 Replies)
Discussion started by: kamlesh_p
2 Replies

10. Shell Programming and Scripting

Check directory space?

Is there some command I can use to check to see if there is 2 Gig of space available in a directory before I created a 2 Gig file? (3 Replies)
Discussion started by: lesstjm
3 Replies
Login or Register to Ask a Question