Split column into two on first occurence of any number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Split column into two on first occurence of any number
# 1  
Old 07-07-2015
Wrench Split column into two on first occurence of any number

Input :
Code:
abc def 1 xyz zzz
bca cde 2 yyy xxx

Expected output :
Code:
abc def |1 xyz zzz
bca cde |2 yyy xxx

I have tried the command below and losing the number. Any help is greatly appreciated

1.
Code:
 sed 's/[0-9]/|/' num.txt

Result:
Code:
            abc def | xyz zzz
            bca cde | yyy xxx

2.
Code:
 sed 's/^[^0-9]*/|/' num.txt

Result:
Code:
             |1 xyz zzz
             |2 yyy xxx



Moderator's Comments:
Mod Comment Please use next time code tags, for your code and data, thanks

Last edited by vbe; 08-06-2015 at 08:15 AM..
# 2  
Old 07-07-2015
try This :

Code:
sed -e 's/\(.*\)\([[:digit:]]\)\(.*\)/\1\|\2\3/' file

# 3  
Old 07-07-2015
Tried it and and gives the output same as input.

abc def 1 xyz zzz
bca cde 2 yyy xxx
# 4  
Old 07-07-2015
Strange ,I am getting below

Code:
$cat a
abc def 1 xyz zzz
bca cde 2 yyy xxx
$sed -e 's/\(.*\)\([[:digit:]]\)\(.*\)/\1\|\2\3/' a
abc def |1 xyz zzz
bca cde |2 yyy xxx
$

This User Gave Thanks to Khanaza For This Post:
# 5  
Old 07-07-2015
Hi.

The sed utility has a useful construct for what was matched, the ampersand, "&":
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate augmented replacement, sed.

# Infrastructure, functions, debugging, printing.
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C sed

FILE=${1-data1}

pl " Input file $FILE:"
cat $FILE

pl " Results:"
sed 's/[0-9][0-9]*/|&/' $FILE

exit 0

producing:
Code:
$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian 5.0.8 (lenny, workstation) 
bash GNU bash 3.2.39
sed GNU sed version 4.1.5

-----
 Input file data1:
abc def 1 xyz zzz
bca cde 2 yyy xxx
ghi jkl 99999 pqr stu

-----
 Results:
abc def |1 xyz zzz
bca cde |2 yyy xxx
ghi jkl |99999 pqr stu

Best wishes ... cheers, drl
This User Gave Thanks to drl For This Post:
# 6  
Old 07-07-2015
Code:
$ cat test.file
abc def 1 xyz zzz
bca cde 2 yyy xxx
bca cde 2 yyy xxx bca cde 2 yyy xxx
abc def 1 xyz zzz abc def 1 xyz zzz

perl -pe 's/\d/\|$&/' test.file
abc def |1 xyz zzz
bca cde |2 yyy xxx
bca cde |2 yyy xxx bca cde 2 yyy xxx
abc def |1 xyz zzz abc def 1 xyz zzz

This User Gave Thanks to Aia For This Post:
# 7  
Old 07-07-2015
SmilieThank you all.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk split columns to row after N number of column

I want to split this with every 5 or 50 depend on how much data the file will have. And remove the comma on the end Source file will have 001,0002,0003,004,005,0006,0007,007A,007B,007C,007E,007F,008A,008C Need Output from every 5 tab and remove the comma from end of each row ... (4 Replies)
Discussion started by: ranjancom2000
4 Replies

2. Shell Programming and Scripting

Print occurence number

Hi folks, I have a file with lots of lines in a text file,i need to print the occurence number after sorting based on the first column as shown below, thanks in advance. sam,dallas,20174 sam,houston,20175 sam,atlanta,20176 jack,raleigh,457865 jack,dc,7845 john,sacramento,4567 ... (4 Replies)
Discussion started by: tech_frk
4 Replies

3. Shell Programming and Scripting

Split column data if the table has n number of column's with some record

Split column data if the table has n number of column's with some record then how to split n number of colmn's line by line with records Table --------- Col1 col2 col3 col4 ....................col20 1 2 3 4 .................... 20 a b c d .................... v ... (11 Replies)
Discussion started by: Priti2277
11 Replies

4. Shell Programming and Scripting

Split column data if the table has n number of column's

please write a shell script Table -------------------------- 1 2 3 a b c 3 4 5 c d e 7 8 9 f g h Output should be like this --------------- 1 2 3 3 4 5 7 8 9 a b c c d e f g h (1 Reply)
Discussion started by: Priti2277
1 Replies

5. UNIX for Dummies Questions & Answers

Add number of occurence

Good morning, I need help to add number of occurence based on column 1 & column 5 file input 81161267334|1|100000|81329998077|20150902 81161267334|1|50000|82236060161|20150902 81161268637|1|25000|81329012229|20150911 81161269307|1|25000|81327019134|20150901... (3 Replies)
Discussion started by: radius
3 Replies

6. Shell Programming and Scripting

Split a fixed length file bases on last occurence of string

Hi, I need to split a file based on last occurece of a string. PFB the explanation I have a file in following format aaaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbb ccccccccccccccccccccccccccc ddddddddddddddddddddddddddd 3186rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr... (4 Replies)
Discussion started by: Neelkanth
4 Replies

7. Shell Programming and Scripting

awk code to ignore the first occurence unknown number of rows in a data column

Hello experts, Shown below is the 2 column sample data(there are many data columns in actual input file), Key, Data A, 1 A, 2 A, 2 A, 3 A, 1 A, 1 A, 1 I need the below output. Key, Data A, 2 A, 2 A, 3 A, 1 A, 1 A, 1 (2 Replies)
Discussion started by: ks_reddy
2 Replies

8. Shell Programming and Scripting

Number each occurence using sed

hi, I need to number each occurrence of a pattern within a file using sed. Given object 0000 object 111 object 222 I need following 1.object 0000 2.object 111 3.object 222 (5 Replies)
Discussion started by: xerox
5 Replies

9. Shell Programming and Scripting

Split single file into multiple files based on the number in the column

Dear All, I would like to split a file of the following format into multiple files based on the number in the 6th column (numbers 1, 2, 3...): ATOM 1 N GLY A 1 -3.198 27.537 -5.958 1.00 0.00 N ATOM 2 CA GLY A 1 -2.199 28.399 -6.617 1.00 0.00 ... (3 Replies)
Discussion started by: tomasl
3 Replies

10. UNIX for Dummies Questions & Answers

grep the number of first occurence

File1.txt ....... ....... OMC LA OMC LK OMC LS ........ ........ Above is the content of File1.txt, i want to get the Number of Occurence to order, lets say if OMC LA = 1, OMC LS=3, and OMC LK=2.. omc_ident="OMC LA" or "OMC LK" or "OMC LS" omc_num=`grep '^OMC' File1.txt| grep... (4 Replies)
Discussion started by: neruppu
4 Replies
Login or Register to Ask a Question