Need Help to print a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need Help to print a file
# 1  
Old 03-21-2012
Data Need Help to print a file

Hi,

I need help in printing a file, the original file contains like below

Code:
TN_SLM_43307_02/r01s1b01p15c01  862065
UW_TN_W_1323/r01s1b01p15c01     862048
S_TN_CHN_26_02/r01s1b01p15c01   1118980
UW_TN_W_1837/r01s1b01p15c01     862079

I need output like this
Code:
‘TN_SLM_43307_02/r01s1b01p15c01’ 862065
‘UW_TN_W_1323/r01s1b01p15c01’  862048
‘S_TN_CHN_26_02/r01s1b01p15c01‘ 1118980
‘UW_TN_W_1837/r01s1b01p15c01’  862079

Means 1st column should come in ‘ ‘ .

Please help me…..
Smilie

Last edited by radoulov; 03-21-2012 at 04:55 PM.. Reason: Code tags, please!
# 2  
Old 03-21-2012
Code:
sed "s/\([^ ][^ ]*\)\(.*\)/'\1'\2/" infile

# 3  
Old 03-22-2012
Code:
$ while read a b; do echo -e "'$a'\t$b"; done < test.txt
'TN_SLM_43307_02/r01s1b01p15c01'        862065
'UW_TN_W_1323/r01s1b01p15c01'   862048
'S_TN_CHN_26_02/r01s1b01p15c01' 1118980
'UW_TN_W_1837/r01s1b01p15c01'   862079

# 4  
Old 03-22-2012
Code:
sed "s/[^ \t]*/'&'/" infile

Code:
awk '$1=q $1 q' q=\' infile

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 03-22-2012
Just to explain that my point of using [^ ][^ ]* instead of [^ ]* was to avoid matching 0 characters.
This is a corner case, of course - if the input happens to contain empty lines,
for example:

Code:
% sed "s/[^ \t]*/'&'/" infile               
'TN_SLM_43307_02/r01s1b01p15c01'  862065
''
'UW_TN_W_1323/r01s1b01p15c01'     862048
'S_TN_CHN_26_02/r01s1b01p15c01'   1118980
'UW_TN_W_1837/r01s1b01p15c01'     862079

And, of course, the second group \(.*\) in my code is unnecessary:

Code:
% sed "s/[^ \t][^ \t]*/'&'/" infile
'TN_SLM_43307_02/r01s1b01p15c01'  862065

'UW_TN_W_1323/r01s1b01p15c01'     862048
'S_TN_CHN_26_02/r01s1b01p15c01'   1118980
'UW_TN_W_1837/r01s1b01p15c01'     862079

With sed implementations that support EREs:

Code:
% sed -r "s/[^ \t]+/'&'/" infile
'TN_SLM_43307_02/r01s1b01p15c01'  862065

'UW_TN_W_1323/r01s1b01p15c01'     862048
'S_TN_CHN_26_02/r01s1b01p15c01'   1118980
'UW_TN_W_1837/r01s1b01p15c01'     862079

Some sed implementations recognize the Perl notation:

Code:
% sed -r "s/\S+/'&'/" infile
'TN_SLM_43307_02/r01s1b01p15c01'  862065

'UW_TN_W_1323/r01s1b01p15c01'     862048
'S_TN_CHN_26_02/r01s1b01p15c01'   1118980
'UW_TN_W_1837/r01s1b01p15c01'     862079

# 6  
Old 03-22-2012
That is good point, indeed I also thought that the presence of empty lines would be less likely.. In addition to radoulov's suggestions, here is an awk version:

Code:
awk 'NF{$1=q $1 q}1' q=\' infile


Last edited by Scrutinizer; 03-22-2012 at 05:27 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use while loop to read file and use ${file} for both filename input into awk and as string to print

I have files named with different prefixes. From each I want to extract the first line containing a specific string, and then print that line along with the prefix. I've tried to do this with a while loop, but instead of printing the prefix I print the first line of the file twice. Files:... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

2. UNIX for Dummies Questions & Answers

Reading Xml file and print the values into the text file in columnwise?

hi guys, i want help... Reding XML file and print the values into the text file using linux shell script file as per below xml file <sequence> <Filename>aldorzum.doc</Filename> <DivisionCode>US</DivisionCode> <ContentType>Template</ContentType> <ProductCode>VIMZIM</ProductCode> </sequence>... (4 Replies)
Discussion started by: sravanreddy
4 Replies

3. UNIX for Dummies Questions & Answers

Reading XML file and print the values in the text file using Linux shell script

hi guys, i want help... Reding XML file and print the values into the text file using linux shell script file as per below xml file <sequence> <Filename>aldorzum.doc</Filename> <DivisionCode>US</DivisionCode> <ContentType>Template</ContentType> <ProductCode>VIMZIM</ProductCode> </sequence>... (1 Reply)
Discussion started by: sravanreddy
1 Replies

4. Shell Programming and Scripting

How to print excel file / csv file in solaris server ?

Hi, I have to print a excel file in solaris server. I am using lexmark printer with post script printer driver . But the problem is when i am using " lp -d <printer name> <somefile name.xls>" command it is not printing correctly. but for pdf file using the same command it printing correctly. ... (2 Replies)
Discussion started by: Sambuddha
2 Replies

5. Shell Programming and Scripting

Strings from one file which exactly match to the 1st column of other file and then print lines.

Hi, I have two files. 1st file has 1 column (huge file containing ~19200000 lines) and 2nd file has 2 columns (small file containing ~6000 lines). ################################# huge_file.txt a a ab b ################################## small_file.txt a 1.5 b 2.5 ab ... (4 Replies)
Discussion started by: AshwaniSharma09
4 Replies

6. Shell Programming and Scripting

Howto Print File Path or Print the Filename

I'm trying to clean up my samba share and need to print the found file or print the path of the image it tried to searched for. So far I have this but can't seem to get the logic right. Can anyone help point me in the right direction? for FILE in `cat list`; do if ; then ... (1 Reply)
Discussion started by: overkill
1 Replies

7. UNIX for Dummies Questions & Answers

find locked files, print file path, unlock file

using OS X and the Terminal, I'd like to find all locked files in a specified directory, unlock them, and print a list of those files that were unlocked how can I do this? I'm familiar with chflags nouchg for unlocking one file but not familiar with unix enough to do what I'd like. Thanks! (0 Replies)
Discussion started by: alternapop
0 Replies

8. Shell Programming and Scripting

Need shell script to read two file at same time and print out in single file

Need shell script to read two file at same time and print output in single file Example I have two files 1) file1.txt 2) file2.txt File1.txt contains Aaa Bbb Ccc Ddd Eee Fff File2.txt contains Zzz Yyy Xxx (10 Replies)
Discussion started by: sreedhargouda
10 Replies

9. Shell Programming and Scripting

search for the contents in many file and print that file using shell script

hello have a file1 H87I Y788O T347U J23U and file2 J23U U887Y I99U T556U file3 I99O J99T F557J file4 N99I T666U R55Y file5 H87I T347U file6 H77U R556Y E44T file7 Y788O K98U H8I May be using script we can use file1 to search for all the files and have the output H87I file5... (3 Replies)
Discussion started by: cdfd123
3 Replies
Login or Register to Ask a Question