split file content


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting split file content
# 1  
Old 09-09-2011
split file content

Hi All;

I have input file like below
Code:
name char(3)
number number(3)

inputfile
Code:
namenumber
xyz123abc509kai330
aca203
ald390afa000als303

I wanted to split like below:-

output like this:-
Code:
xyz123
abc509
kai330
aca203
ald390
afa000
als303

I used below:-
Code:
awk '{a[$1]=substr($0,1,3)} END {(for i in a) print (i,a[i])}' inputfile

I got only
Code:
xyz123

how to pass variable in susbtr command.

Last edited by Franklin52; 09-09-2011 at 03:25 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 09-09-2011
How about this
Code:
sed 's/.../&\n/g'

# 3  
Old 09-09-2011
Code:
$ cat file
xyz123abc509kai330
aca203
ald390afa000als303

Output:
Code:
$ fold -w6 file
xyz123
abc509
kai330
aca203
ald390
afa000
als303

Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 4  
Old 09-09-2011
Hi Chubler,

Thanks. But it is not working.

---------- Post updated at 11:54 PM ---------- Previous update was at 11:52 PM ----------

Hi Guru,

Great. It is wokring.

---------- Post updated at 11:57 PM ---------- Previous update was at 11:54 PM ----------

Hi Guys,

How to achieve this using awk command.
# 5  
Old 09-09-2011
Code:
$ awk '{i=1;while(i<=length($0)){print substr($0,i,6);i+=6}}'  file
xyz123
abc509
kai330
aca203
ald390
afa000
als303

Guru.
# 6  
Old 09-09-2011
perl mode
Code:
cat test.txt | perl -ne 'chomp; @v = split /(:?.{6})/,$_; map {print( $_ , qq/\n/) if length( $_) } @v'
xyz123
abc509
kai330
aca203
ald390
afa000
als303

---------- Post updated at 01:26 AM ---------- Previous update was at 01:09 AM ----------

for non fixed length, buy same conditions

before:
Code:
xsdfyz123abc50239kasdfai330
aca203cocacola2011
ald390afa000als303

Code:
cat test.txt | perl -ne "chomp; @v = split /(:?[a-z]+\d+)/i,$_; map {print( $_ , qq/\n/) if length( $_) } @v"
xsdfyz123
abc50239
kasdfai330
aca203
cocacola2011
ald390
afa000
als303


Last edited by Franklin52; 09-09-2011 at 03:29 AM.. Reason: Please use code tags for code and data samples, thank you
# 7  
Old 09-09-2011
Code:
sed -n 's/[a-zA-Z]*[0-9]*/&\n/gp' input | sed '/^$/d'


Last edited by ltomuno; 09-09-2011 at 07:00 AM.. Reason: Please use code tags for code and data samples, thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Split content based on keywords

I need to split the file contents with multiple rows based on patterns Sample: Input: ABC101testXYZ102UKMNO1092testing ABC999testKMNValid Output: ABC101test XYZ102U KMN1092testing ABC999test KMNValid In this ABC , XYZ and KMN are patterns Continue here./mod] Please read forum... (1 Reply)
Discussion started by: Jairaj
1 Replies

2. Shell Programming and Scripting

Split a content in a file with specific interval base on the delimited values using UNIX command

Hi All, we have a requirement to split a content in a text file every 5 rows and write in a new file . conditions: if 5th line falls between center of the statement . it should look upto after ";" files are below format: 1 UPDATE TABLE TEST1 SET VALUE ='AFDASDFAS' 2 WHERE... (3 Replies)
Discussion started by: KK230689
3 Replies

3. Shell Programming and Scripting

awk : split file and rename and save in path according to content

Hello, I'm using Windows 7 ; sed, awk and gnuwin32 are installed. I have a big text file I need to manipulate. In short, I will have to split it in thousands of short files, then rename and save in a folder which name is based upon filename. Here is a snippet of my big input.txt file (this... (4 Replies)
Discussion started by: sellig
4 Replies

4. Shell Programming and Scripting

Split a file in more files based on score content

Dear All, I have the following file tabulated: ID distanceTSS score 8434 571269 10 10122 393912 9 7652 6 10 4863 1451 9 8419 39 2 9363 564 21 9333 7714 22 9638 8334 9 1638 1231 11 10701 918 1000 6587 32056 111 What I would like to do is the following, create 100 new files based... (5 Replies)
Discussion started by: paolo.kunder
5 Replies

5. Shell Programming and Scripting

split file content into specific folders

Hi I have a large text file and I want to split its content into multiple flies. this large file contains several blocks of codes separated by a comment line for each block. this comment line represents a directory path So, when separate these blocks each into a separate file, This output... (7 Replies)
Discussion started by: turki_00
7 Replies

6. Shell Programming and Scripting

split content and write to new record

Hi, Help required to split record value and write to new row. Input a~b~c~value in ('3','4','5')~test output a~b~c~3~test a~b~c~4~test a~b~c~5~test input a~b~c~value in ('3','4')~test output a~b~c~3~test a~b~c~4~test (8 Replies)
Discussion started by: Jairaj
8 Replies

7. Shell Programming and Scripting

Split the file based on the content

Arun kumar something somehting Enterting in to the line . . . . Some text text Finshing the sentence Some other text . . . . Again something somehting Enterting in to the line . . . . . . Again text text Finshing the sentence (6 Replies)
Discussion started by: arukuku
6 Replies

8. Shell Programming and Scripting

Help with split one file content into multiple different file

Input file: cat input_file.txt data_1 2342 data_3 242 data_1 3546 data_5 458 data_10 342 data_30 42 data_10 346 content_50 48 content_1 2343 Desired output: cat output_file1.txt data_1 2342 data_3 242 data_1 3546 data_5 458 (2 Replies)
Discussion started by: perl_beginner
2 Replies

9. Shell Programming and Scripting

Help with split the file content into multiple different file

Input file content: NA_10001 XA_10081 NG_10015 AC_1321.1 . . Desired output file: ls *.txt NA_10001.txt XA_10081.txt NG_10015.txt AC_1321.1.txt cat NA_10001.txt NA_10001 cat XA_10081.txt XA_10081 (1 Reply)
Discussion started by: perl_beginner
1 Replies

10. Shell Programming and Scripting

split file depending on content

Hi, I have a file which contains records of data. I need to split the file into multiple files depending upon the value of last field. How do i read the last field of each record in the file??? Regards, Chaitrali (4 Replies)
Discussion started by: Chaitrali
4 Replies
Login or Register to Ask a Question