How to insert tab at specified column.HELP


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to insert tab at specified column.HELP
# 1  
Old 09-20-2007
How to insert tab at specified column.HELP

I have input like:
1234567890

cut -c1-3,6-7,9-10 input > output

Now I got 1236790.

I want to insert space between each cut. So the output like:
123 67 90

Can anybody help?
Thanks.
# 2  
Old 09-20-2007
One way to add spaces (not a tab or "\t" character):
Code:
awk '{print substr($0,1,3), substr($0,6,2), substr($0, 9,2)  }' > output

# 3  
Old 09-20-2007
Is there other way using other UNIX command? awk is not working for me.
giving me "16 bit MS-DOS Subsystem" error message. Smilie
# 4  
Old 09-20-2007
Hi.

Quote:
Originally Posted by sslr
I want to insert space between each cut. So the output like:
123 67 90
This is one alternative using GNU cut:
Code:
#!/usr/bin/env sh

# @(#) s1       Demonstrate insertion of string for cut output fields.

set -o nounset
echo

## Use local command version for the commands in this demonstration.

echo "(Versions of codes used in this script -- local code \"version\")"
version bash cut

echo
echo " Expecting |123 67 90|"

echo "1234567890" |
cut -c1-3,6-7,9-10 --output-delimiter=" "

exit 0

Producing:
Code:
% ./s1

(Versions of codes used in this script -- local code "version")
GNU bash, version 2.05b.0(1)-release (i386-pc-linux-gnu)
cut (coreutils) 5.2.1

 Expecting |123 67 90|
123 67 90

See man pages for details ... cheers, drl

Last edited by drl; 09-20-2007 at 06:06 PM..
# 5  
Old 09-21-2007
Smilie
Thank you drl. You are so helpful.
# 6  
Old 09-21-2007
What if two field being cut are next to each other, the delimiter will not be inserted?
cut -c1-3,4-5 --output-delimiter="|" input > output
The output will be:
12345

How to get 123|45?

Another question is how to get rid of the leading space, if I have input like:
1234567890
I'd like to have output
123|45
instead of
123|45

Could you please help?
Thanks.
# 7  
Old 09-21-2007
Oops.
input: | 123456789|
output:|123|45|

no leading or tailing space in every field.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace a column in tab delimited file with column in other tab delimited file,based on match

Hello Everyone.. I want to replace the retail col from FileI with cstp1 col from FileP if the strpno matches in both files FileP.txt ... (2 Replies)
Discussion started by: YogeshG
2 Replies

2. Shell Programming and Scripting

Converting Single Column into Multiple rows, but with strings to specific tab column

Dear fellows, I need your help. I'm trying to write a script to convert a single column into multiple rows. But it need to recognize the beginning of the string and set it to its specific Column number. Each Line (loop) begins with digit (RANGE). At this moment it's kind of working, but it... (6 Replies)
Discussion started by: AK47
6 Replies

3. UNIX for Dummies Questions & Answers

Vi - insert a tab between words?

I have several lines in a file that I want to replace a space with a tab. For example: 111047 Julie Jones email@email.com 111047 Julie Jones email@email.com I want to replace the space after the word "jones" with a tab. How do I achieve that in Vi? Please assist. Thanks! (5 Replies)
Discussion started by: onlinelearner02
5 Replies

4. UNIX for Dummies Questions & Answers

insert whitespace (tab) at start of each line

Hi all, I have this: begin data; dimensions nind=168 nloci=6; info BDT001.4 ( 1 , 1 ) ( 1 , 12 ) BDT003.4 ( 1 , 1 ) ( 12 , 12 ) BDT007.4 ( 1 , 1 ) ( 12 , 12 ) BDT009.4 ( 1 , 32 ) ( 12 , 22 ) etc, etc And need this: begin data; dimensions nind=168 nloci=6; info ... (2 Replies)
Discussion started by: MDeBiasse
2 Replies

5. Shell Programming and Scripting

insert LF and TAB for formatting

trying to insert a LF and 2 TABs for this: sed 's/<td><\/td>/<td>\n\t\t<\/td>/' infile. but, I'm not getting the syntax for inserting the LF and TABs correct (1 Reply)
Discussion started by: dba_frog
1 Replies

6. UNIX for Dummies Questions & Answers

Insert Field into a tab-delimited file

Hello, I have about 100 files in a directory with fields which are tab delimited. I would like to append the file name as the first field and it has to be done as many times as the total lines in the file. For example, myFile1.txt has the following data: 1 x y z 2 a b ... (5 Replies)
Discussion started by: Gussifinknottle
5 Replies

7. Shell Programming and Scripting

insert a field into a tab delimited file

Hello, Can someone help me to do this with awk or sed? I have a file with multiple lines, each line has many fields separated with a tab. I would like to add one more field holding 'na' in between the first and second fields. old file looks like, 1, field1 field2 field3 ... 2, field1... (7 Replies)
Discussion started by: ssshen
7 Replies

8. Shell Programming and Scripting

sed: how to insert tab?

Hi, I'm using the following to insert lines into file: sed ${rowNr}i'\ first row\ second row\ third row\ ' file.txt How can I add tab in front of each added line? "\t" or actual TAB does not seem to work? Thanks! (2 Replies)
Discussion started by: Juha
2 Replies

9. Shell Programming and Scripting

pls help me to insert tab and formatthe file

i want to format the file with tab delimitaed and assign heading to each column . my format of file is 7 aiss 10 8 linux 25 9 linux_10for 35 Ouput i want like this Ver Host Fails 7 aiss 10 8 linux 25 9 ... (5 Replies)
Discussion started by: getdpg
5 Replies

10. Shell Programming and Scripting

Insert TAB in echo statement

Hi, Can some1 help me to output a tab in an echo statement. I have tried echo "RNC: \t NODEB" but dont get the correct output. I am a beginnger to unix, so pls hold back the laughs....if u can (5 Replies)
Discussion started by: sunils27
5 Replies
Login or Register to Ask a Question