Using cut command in a fixed length file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using cut command in a fixed length file
# 1  
Old 02-13-2010
Using cut command in a fixed length file

Hi,

I have a file which have set of rows and has to create separate files based on the id.

Eg:
Code:
     001_AHaris020
     001_ATony030
     002_AChris090
     002_ASmit060
     003_AJhon001

Output: I want three files like 001_A.txt, 002_A.txt and 003_A.txt.

001_A.txt should have

Code:
     Haris020
     Tony030

002_A.txt should have

Code:
     Chris090
     Smit060

003_A.txt should have
Code:
      Jhon001

Please give some ideas using cut command having in the while loop...

Last edited by Franklin52; 02-13-2010 at 01:41 PM.. Reason: Please use code tags!
# 2  
Old 02-13-2010
cut command in while loop

Code:
while read line;do
    grep "001" $line|awk -F "_[0-9]" '{print $NF}' >> File1.txt
    grep "002" $line|awk -F "_[0-9]" '{print $NF}' >> file2.txt
done<Inputfile


Last edited by Franklin52; 02-13-2010 at 01:41 PM.. Reason: Please use code tags!
# 3  
Old 02-13-2010
Code:
file1:
001_AHaris020
001_ATony030
002_AChris090
002_ASmit060
003_AJhon001

while read LINE; do
  echo $LINE | cut -c6- >> $(echo $LINE | cut -c1-5).txt
done < file1

$ cat 001_A.txt 
Haris020
Tony030

$ cat 002_A.txt 
Chris090
Smit060

$ cat 003_A.txt 
Jhon001

# 4  
Old 02-14-2010
thx

tHANKS

Last edited by techmoris; 02-14-2010 at 01:13 PM..
# 5  
Old 02-14-2010
Or using parameter expansion (without cut).
Code:
while read line;do echo ${line:5} >> ${line:0:5}.txt;done < infile

... faster and shorter Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Fixed Length file from a SQL script

Hi, I have a DB2 UDB 9.7 SQL script, as follows: I need to pass the script into Unix and generate a fixed length file from this. Can someone kindly provide a script to achieve it? SELECT CAST(COALESCE(CL_ID,'000000000') AS CHAR(9)) AS CL_ID ,STATUS... (5 Replies)
Discussion started by: ebsus
5 Replies

2. UNIX for Dummies Questions & Answers

Length of a fixed width file

I have a fixed width file of length 53. when is try to get the lengh of the record of that file i get 2 different answers. awk '{print length;exit}' <File_name> The above code gives me length 50. wc -L <File_name> The above code gives me length 53. Please clarify on... (2 Replies)
Discussion started by: Amrutha24
2 Replies

3. Shell Programming and Scripting

Splitting fixed length file using awk

Hi, I need to split a fixed length file of 160 characters based on value of a column. Example: ABC 456780001 DGDG SDFSF BCD 444440002 SSSS TTTTT ABC 777750003 HHHH UUUUU THH 888880001 FFFF LLLLLL HHH 999990002 GGGG OOOOO I need to split this file on basis of column from... (7 Replies)
Discussion started by: Neelkanth
7 Replies

4. Shell Programming and Scripting

Replace Date in a fixed length file

Hello All, I working on ksh. I am using fixed length file. My file is like: ======== IXTTIV110827 NANTH AM IKSHIT ABCDEF 0617 IJAY NAND EENIG ZXYWVU 0912 AP OOK OONG PQRSTU100923 NASA DISH TTY ASDFG 0223 GHU UMA LAM QWERT 0111 ATHE SH THEW ======= From 7th to 12 is a date... (4 Replies)
Discussion started by: AnanthaDikshit
4 Replies

5. UNIX for Dummies Questions & Answers

Convert a tab delimited/variable length file to fixed length file

Hi, all. I need to convert a file tab delimited/variable length file in AIX to a fixed lenght file delimited by spaces. This is the input file: 10200002<tab>US$ COM<tab>16/12/2008<tab>2,3775<tab>2,3783 19300978<tab>EURO<tab>16/12/2008<tab>3,28523<tab>3,28657 And this is the expected... (2 Replies)
Discussion started by: Everton_Silveir
2 Replies

6. UNIX for Dummies Questions & Answers

What the command to find out the record length of a fixed length file?

I want to find out the record length of a fixed length file? I forgot the command. Any body know? (9 Replies)
Discussion started by: tranq01
9 Replies

7. Shell Programming and Scripting

convert fixed length file to CSV

Newbie Looking for a script to convert my input file to delimited text file. Not familier with AWK or shell programing. Below is sample record in my input file and the expected output format. My OS is HPUX 11.23. Thanks in advance for your assistance. tbtbs input file:... (12 Replies)
Discussion started by: tbtbs
12 Replies

8. Shell Programming and Scripting

Manipulating a fixed length file w/o PERL

Greetings, I need to take a fixed length file, similar to the following: <input file> 1233 e 612 i 43378 f 03 x 22 17 e 9899 a 323e a6 z7 read in the character in position 6, and if that character = e, delete that line from the file. <output file> 43378 f 03 x 22 17 e 9899 ... (4 Replies)
Discussion started by: dabear
4 Replies

9. UNIX for Dummies Questions & Answers

is LS a fixed length? I want to use cut -c#-# to isolate filename.

-rw-r--r-- 1 fxpbftp fusion 368 Jun 10 08:34 FX_1.11840235236594E12.111234236809956 If I have a long list of files that look like this (they al begni with FX_1.#######.####) Sometimes, there may be less numbers or more in the filename, that varies. I wish to isolate just the... (8 Replies)
Discussion started by: yongho
8 Replies

10. Shell Programming and Scripting

creating a fixed length output from a variable length input

Is there a command that sets a variable length? I have a input of a variable length field but my output for that field needs to be set to 32 char. Is there such a command? I am on a sun box running ksh Thanks (2 Replies)
Discussion started by: r1500
2 Replies
Login or Register to Ask a Question