Visit Our UNIX and Linux User Community


Issue while splitting a row of record


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issue while splitting a row of record
# 1  
Old 09-22-2009
Issue while splitting a row of record

Hi,

I have one file with the following details,


Code:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Account_Id Date Id Balance
44 9 1000.00 30 15-10-2173 10 1000.00 42 15-10-2173 10 1200.00 53 01-01-2008 10 1200.00

I need to split up the values in to the respective fields as follows,

Code:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Account_Id Date Id Balance
44         9 1000.00 
30 15-10-2173  10 1000.00 
42 15-10-2173  10 1200.00 
53 01-01-2008  10 1200.00

Initially i used the following command to split up the 4 values.

Code:
awk 'BEGIN{RS=" "}{sub("\n","")}ORS=NR%4?RS:"\n"'inpFile >tempFile

But in some cases I will not get the exact 4 values in the input file as given above(date is missing in the first record).

At that time the above command is displaying the unexpected result.

Can anyone help me on this.

Thanks in advance..!


Last edited by vgersh99; 09-22-2009 at 07:38 AM.. Reason: code tags, PLEASE!
# 2  
Old 09-22-2009
Code:
tr '.' '\n' <input>>output



---------- Post updated at 01:15 AM ---------- Previous update was at 01:04 AM ----------

Sorry i think this script leave 00.

Last edited by repinementer; 09-22-2009 at 06:09 AM..
# 3  
Old 09-22-2009
Code:
cat you_file  | sed "s/\.00/.00\n/"  |awk '{
        if (NF > 4 )
        {
                conta=1;
                for (word=1;word <=NF; word++)
                {
                        if (conta == 5)
                        {
                                printf "\n"
                                conta=1;
                        }
                        printf $word " ";
                        conta+=1;

                }
        }
        else print $0
     }
   END { }'

# 4  
Old 09-22-2009
Hi..
Thanks alot for your reply..

Here if am using the above code snippet, its giving the output like this,
Code:
Account_Id Date Id Balance
44         9 1000.00 n
30 15-10-2173  10 1000.00 
42 15-10-2173  10 1200.00 
53 01-01-2008  10 1200.00

'n' is coming after the balance amount.

Can you please check this.

Last edited by vgersh99; 09-22-2009 at 07:39 AM.. Reason: code tags, PLEASE!
# 5  
Old 09-22-2009
sorry in my execution i don't obtain the 'n' character.
maybe your data-source file is diferent at the text you put ?

Code:
$more kk.txt 
Account_Id Date Id Balance
44 9 1000.00 30 15-10-2173 10 1000.00 42 15-10-2173 10 1200.00 53 01-01-2008 10 1200.00
$sh kk2.sh
Account_Id Date Id Balance
44 9 1000.00
30 15-10-2173 10 1000.00 
42 15-10-2173 10 1200.00
53 01-01-2008 10 1200.00

# 6  
Old 09-22-2009
Hi ..
I executed the same script as you given above.but then I am getting the report like,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Code:
Account_Id Date Id Balance
44  9  1000.00n  30
15-10-2173  10  1000.00  42
15-10-2173  10  1200.00  53
01-01-2008  10  1200.00

Then I tried to execute this with one small change in your script,
Code:
sed "s/\.00/.00 \n/" |awk '{ (Just given a single space in between .00 and \n )

Then I am getting the result as follows,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Code:
Account_Id Date Id Balance
44  9  1000.00  n
30  15-10-2173  10  1000.00
42  15-10-2173  10  1200.00
53  01-01-2008  10  1200.00


Last edited by vgersh99; 09-22-2009 at 07:40 AM.. Reason: code tags, PLEASE!
# 7  
Old 09-22-2009
To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

Previous Thread | Next Thread
Test Your Knowledge in Computers #786
Difficulty: Medium
In 2019, Bill Gates had a net worth estimated to be over $200 billion USD.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Splitting single row into multiple rows based on for every 10 digits of last field of the row

Hi ALL, We have requirement in a file, i have multiple rows. Example below: Input file rows 01,1,102319,0,0,70,26,U,1,331,000000113200000011920000001212 01,1,102319,0,1,80,20,U,1,241,00000059420000006021 I need my output file should be as mentioned below. Last field should split for... (4 Replies)
Discussion started by: kotra
4 Replies

2. Shell Programming and Scripting

How to place Record in a row as a column name?

Hi, I want to know how to place a particular field availble in a record as the columnname. Name,Sam,Age,13,School, StJohn Name,Rita,Age,12,School, StMichael, Name,John,Age,14,School, StAnthony I want the output as Name Age School Sam 13 StJohn Rita 12 StMichael John 14... (6 Replies)
Discussion started by: sidnow
6 Replies

3. Shell Programming and Scripting

Exclude the header row while splitting the file

Hi All, i have script like ... "TYPE_ID" "ID" "LIST_ID" "18" "52010" "1059" "18" "52010" "1059" "18" "52010" "1059" "18" "52010" "1059" i am using the below code it's not taking the header row. awk -F"\t" -v file=test1.txt -v file1=test2.txt ' { if(... (7 Replies)
Discussion started by: bmk
7 Replies

4. Shell Programming and Scripting

Splitting record into multiple records by appending values from an input field (AWK)

Hello, For the input file, I am trying to split those records which have multiple values seperated by '|' in the last input field, into multiple records and each record corresponds to the common input fields + one of the value from the last field. I was trying with an example on this forum... (4 Replies)
Discussion started by: imtiaz99
4 Replies

5. Shell Programming and Scripting

Need help splitting huge single record file

I was given a data file that I need to split into multiple lines/records based on a key word. The problem is that it is 2.5GB or bigger and everything I try in perl or sed causes a Segmentation fault. Can someone give me some other ideas. The data is of the form:... (5 Replies)
Discussion started by: leolson
5 Replies

6. Shell Programming and Scripting

Splitting data from one row as multiple columns

Hi I have a file containing some data as follows: 11-17-2010:13:26 64 4 516414 1392258 11-17-2010:13:26 128 4 586868 695603 11-17-2010:13:26 256 4 474937 1642294 11-17-2010:13:32 64 4 378715 1357066 11-17-2010:13:32 128 4 597981 1684006 ... (17 Replies)
Discussion started by: annazpereira
17 Replies

7. UNIX for Dummies Questions & Answers

Record splitting with AWK

Hi all ! I need your help as quick as possible. My input file like this: bạc těnh ( 薄情) 1 . 薄情な.2. 夫婦或いは男女の不貞を指す。 bách (百,迫)1.100ドソ. tr a m b a c ともいう. 2.柏(カヽしわ)・ 3.圧迫する.4.差し迫った, My propose is take the value in the firt bracket. I used the command like : ...if (index(... (6 Replies)
Discussion started by: maixu134
6 Replies

8. Shell Programming and Scripting

Split a record ( in a row) and make it as new row

Hi All, The following is the scenario id name -------------- 1 William;Johnson 2 Azim;Abdul 3 Grasim . . etc.... I need the following output id name -------------- 1 William 1 Johnson 2 Azim (2 Replies)
Discussion started by: kottursamy
2 Replies

9. Shell Programming and Scripting

splitting a record and adding a record to a file

Hi, I am new to UNIX scripting and woiuld appreicate your help... Input file contains only one (but long) record: aaaaabbbbbcccccddddd..... Desired file: NEW RECORD #new record (hardcoded) added as first record - its length is irrelevant# aaaaa bbbbb ccccc ddddd ... ... ... (1 Reply)
Discussion started by: rsolap
1 Replies

10. UNIX for Advanced & Expert Users

Issue with Splitting of file

HI! All Iam running this script ro split the file ,attact a timesatmp to it and then conver it to .tmp extension cd /home/staff/thussain Prefix=Z_PRICE_NEW_`date "+%Y%m%d%H%M%S"` split -3000 -a 5 /home/staff/thussain/Z_PRICE_NEW.txt $Prefix find . -name "$Prefix*" -print | { while read... (6 Replies)
Discussion started by: mohdtausifsh
6 Replies

Featured Tech Videos