split character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting split character
# 1  
Old 09-29-2012
split character

how we can split characters of a field? for example if we have a file such like below:
Code:
...
20120102 120352.5AM
...

i want to get out put like this:
Code:
...
2012 01 02 12 03 52.5 AM
...

# 2  
Old 09-29-2012
this is not the best way....

Code:
sed 's/ //g' file | sed -e 's/.\{2\}/& /g' | sed 's/ //1; s/ \./\./;'

# 3  
Old 09-29-2012
This is much more complicated than pamu's sed script, but it won't modify lines unless they have two fields and end in a case insensitive AM or PM; and it will work with one or two digits for the hour in the 2nd field. You didn't give any indication of the format of the lines in your input file represented by the ...'s, so I tried to be a little more cautious:
Code:
awk 'NF == 2 && $2 ~ /[aApP][mM]$/ && (length($2) == 9 || length($2) == 10) {
        l = length($2) == 10
        print substr($1, 1, 4), substr($1, 5, 2), substr($1, 7),
              substr($2, 1, l + 1), substr($2, l + 2, 2), substr($2, l + 4, 4),
              substr($2, l+8)
        #       YYYY            MM              DD
        #       H or HH         mm              ss.s
        #       AM/PM
        next
}
 {      print
}' file

# 4  
Old 09-29-2012
Hi.

The perl language has a built-in function called unpack:
Code:
#!/usr/bin/env perl

# @(#) p1	Demonstrate unpack, in this case characters.

use strict;
use warnings;

my (@a);

# Sample:
# input   20120102 120352.5AM
# output  2012 01 02 12 03 52.5 AM
while (<>) {

  # @a = unpack("a4a2a2xa2a2a4a2");
  @a = unpack("a4 a2 a2 x a2 a2 a4 a2");
  print "a = @a\n";
}

exit(0);

producing:
Code:
% ./p1 data1
a = 2012 01 02 12 03 52.5 AM

This code causes each segment of the input data string to placed in an array element as guided by the template string.

However, perl might not be the most novice-friendly language.

Best wishes ... cheers, drl

Last edited by drl; 09-29-2012 at 05:33 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl split string separated by special character

Hello I have string (string can have more sections) LINE="AA;BB;CC;DD;EE"I would like to assigne each part of string separated by ";" to some new variable. Can someone help? (4 Replies)
Discussion started by: vikus
4 Replies

2. UNIX for Dummies Questions & Answers

Split a column into multiple columns at certain character count

Hey everyone, I have an issue with a client that is passing me a list of values in one column, and occasionally the combination of all the values results in more than an 255 character string. My DB has a 255 character limit, so I am looking to take the column (comma delimited file), and if it... (1 Reply)
Discussion started by: perekl
1 Replies

3. Shell Programming and Scripting

Oneliner ---split string to character by piping shell output to perl

Hello, I was trying to split a string to characters by perl oneliner. echo "The quick brown fox jumps over the lazy dog" | perl -e 'split // ' But did not work as with bash script pipe: echo "The quick brown fox jumps over the lazy dog" | fold -w1 | sort | uniq -ic 8 1 T 1... (6 Replies)
Discussion started by: yifangt
6 Replies

4. Shell Programming and Scripting

awk to split one field and print the last two fields within the split part.

Hello; I have a file consists of 4 columns separated by tab. The problem is the third fields. Some of the them are very long but can be split by the vertical bar "|". Also some of them do not contain the string "UniProt", but I could ignore it at this moment, and sort the file afterwards. Here is... (5 Replies)
Discussion started by: yifangt
5 Replies

5. Shell Programming and Scripting

Unix Perl split special character $

All I'm trying to split a string at the $ into arrays @data:=<dataFile> a $3.33 b $4.44 dfg $0.56 The split command I have been playing with is: split(/\$/, @data) which results with a .33 b .44 dfg .56 any help with this is appreciated /r Rick (9 Replies)
Discussion started by: schultz2146
9 Replies

6. Shell Programming and Scripting

Perl use split and remove specific character

i want to split the input by a space and remove specific characters like full stop, comma...... etc. and then save each word in an array. i got something below, but it didn't work. can anyone please help me? Thank you #!/usr/bin/perl -w while (<>) { $line = <>; @word = split(' ',... (6 Replies)
Discussion started by: mingming88
6 Replies

7. Shell Programming and Scripting

How to split a text after fix character count

HI, I want to split a text after certain fix character count in text. For eg: My file is containing text like: AURBJR,AURCID,AURVID,CHANDV,DAMNEW,DHMMAN,GANGAN,GARKHE,GOREGA,JEJKHA,JEJSHI,JINTUR,JMKKUS,JUNAWA,KALKAL,KHOJEW,KUNJIR,MAGARP,MAHAD, in this i want to print text after each... (5 Replies)
Discussion started by: vikash.rastogi
5 Replies

8. UNIX for Dummies Questions & Answers

How to split a value according to character position

Hello all, I have a script which picks up a series of large numbers, each of which are actually amalgamations of a series of other numbers. Unfortunately there are no separator characters so I can't use awk -F. I am looking for a way of splitting them into variables according to their... (4 Replies)
Discussion started by: michaeltravisuk
4 Replies

9. UNIX for Dummies Questions & Answers

perl split funciton - special character "/"

HI, I have a directory structure. /abc/def/ghi/ I want to store it into array. So that if I do a pop function on that array I can easily go to previous directory. But how can i split and store it. @Directory = split(/\//,$DirectoryVarialbe) That doest works. Any other escape sequence... (5 Replies)
Discussion started by: deepakwins
5 Replies

10. UNIX for Dummies Questions & Answers

Split a file with no pattern -- Split, Csplit, Awk

I have gone through all the threads in the forum and tested out different things. I am trying to split a 3GB file into multiple files. Some files are even larger than this. For example: split -l 3000000 filename.txt This is very slow and it splits the file with 3 million records in each... (10 Replies)
Discussion started by: madhunk
10 Replies
Login or Register to Ask a Question