File in UNIX


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File in UNIX
# 1  
Old 04-23-2013
File in UNIX

Hi,

I need your help in creating a script for the below scenario.
I have a file which has below data.
Code:
Error is : error in dept 10;location 100;
Error is : file has this invalid supplier 10 location 110; dept 10;

Now I need to create two files on the basis of the above data and these files will be for each location.

like
file loc_100 will contain values as below
Code:
error in dept 10

file loc_110 will contain values as below
Code:
file has this invalid supplier 10 location 110 dept 10

So above we will ahve two files loc_100 and loc_110.

Thanks in advance.

Last edited by vbe; 04-23-2013 at 01:38 PM.. Reason: code tagsplease, next time
# 2  
Old 04-23-2013
Code:
awk '
        {
                match ( $0, /location [0-9]*\;/)
                ID = substr ( $0, RSTART, RLENGTH)
                gsub (/location |\;/, X, ID)
                F = "loc_" ID
                gsub (/.*: |\;location.*|\;/, X)
                print > F
                close (F)
        }
' file

This User Gave Thanks to Yoda For This Post:
# 3  
Old 04-23-2013
Many thanks Yoda for this which is working fine.
I want another functionality here.
If the file contains the data as
Code:
Error is : error in dept 10;location 100;
Error is : error in dept 12;location 100;
Error is : file has this invalid supplier 10 location 110; dept 10;

only one file of loc_100 is created which contains the first line "
Code:
Error is : error in dept 10;location 100" inside.

But i want both these lines of location 100 inside loc_100 file.

Thnaks in advance.

Last edited by vbe; 04-23-2013 at 01:39 PM..
# 4  
Old 04-23-2013
If you want both locations, then simply append to file:
Code:
awk '
        {
                match ( $0, /location [0-9]*\;/)
                ID = substr ( $0, RSTART, RLENGTH)
                gsub (/location |\;/, X, ID)
                F = "loc_" ID
                gsub (/.*: |\;location.*|\;/, X)
                print >> F
                close (F)
        }
' file

Make sure you remove the output files that you created previously before running this code.
This User Gave Thanks to Yoda For This Post:
# 5  
Old 05-03-2013
Hi,

Thanks for this which is working fine.
Now i want to add timestamp while createting the file.

like i have a variable fileDate=`date +"%Y%m%d%H%M%S"` which i want to use in awk while creating the file.

But I'm not able to do so.
Can you please help me in this.

Thanks in advance.
# 6  
Old 05-03-2013
try this..
Code:
 
 
awk '
        {
                match ( $0, /location [0-9]*\;/)
                ID = substr ( $0, RSTART, RLENGTH)
                gsub (/location |\;/, X, ID)
                F = "loc_" ID
                gsub (/.*: |\;location.*|\;/, X)
    cmd="date +"%Y%m%d%H%M%S""
    cmd|getline dt
    close(cmd)
    F=F"_"dt
                print >> F
                close (F)
        }
' file

# 7  
Old 05-03-2013
Hi,

Thanks for the reply.
I'm getting the below error.

awk: Cannot divide by zero when using the modulo operator.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Downloading hdfs file to local UNIX through UNIX script

Hi All , I am very new to unix script.I am aware of unix commands but never put together in unix script level.If any one can suggest me technical guidance in the below scenario that will highly beneficial. Data have been already migrated from mainframe to Hadoop file system(HDFS).HDFS server... (15 Replies)
Discussion started by: STCET22
15 Replies

2. What is on Your Mind?

If you're a UNIX file or UNIX file system. What are you and why?

I think i can consider myself as /dev/zero because my unix skills sucks. lol (8 Replies)
Discussion started by: csorhand
8 Replies

3. Shell Programming and Scripting

File Transfer from Window server to UNIX and UNIX to UNIX

Dear All, Can someone help to command or program to transfer the file from windows to Unix server and from one unix server to another Unix server in secure way. I would request no samba client. (4 Replies)
Discussion started by: yadavricky
4 Replies

4. UNIX Desktop Questions & Answers

Can Unix access Windows' File through Command Prompt in Unix

Hi all, I wish to know whether Unix can access window's file in Unix's terminal? Apart from that, how to copy files or share files between Window and Unix? I get to know of secure copy, however, my company's Unix does not support the feature of secure copy? Any other method for me to share/... (5 Replies)
Discussion started by: jessy83
5 Replies

5. Shell Programming and Scripting

UNIX script to FTP file from UNIX server to windows

Hi, I am new to this subject.....Can someone please help me out with the script... unix usernm "sdhftst" unix pwd "chsd13" windows usernm "dfghtst" windows pwd "chsd13" path..../xxx/xxxxx/xxxxxx/xxxxxxx please can u get me a script...its only one file to get ftp. Thanks... (2 Replies)
Discussion started by: himakiran9
2 Replies

6. Solaris

Before I delete any file in Unix, How can I check no open file handle is pointing to that file?

I know how to check if any file has a unix process using a file by looking at 'lsof <fullpath/filename>' command. I think using lsof is very expensive. Also to make it accurate we need to inlcude fullpath of the file. Is there another command that can tell if a file has a truely active... (12 Replies)
Discussion started by: kchinnam
12 Replies

7. Shell Programming and Scripting

Batch job in unix server to move the pdf file from unix to windows.

Hi Experts, I have a requirement where i need to setup a batch job which runs everymonth and move the pdf files from unix server to windows servers. Could some body provide the inputs for this. and also please provide the inputs on how to map the network dirve in the unix like that... (1 Reply)
Discussion started by: ger199901
1 Replies

8. Shell Programming and Scripting

Need to write a script to reformat a file in unix but not familiar with unix

unix script must do the fiollowing open a file containing comma delimited records > each record contains 10 fields > removes the 2nd field and use that same field containing fields 2 to 10 the original record after fprocessing should containing fields 1 and 3 a new erecord must be... (10 Replies)
Discussion started by: dwightja
10 Replies

9. Shell Programming and Scripting

Can we call JSP file from Unix.if so how.Please help me.Im newbie to Unix

Hi Can we call a jsp page from Unix.if so please let me know how we can.. thanks for the help in advance... thanks kumar (0 Replies)
Discussion started by: mailsukumar
0 Replies

10. Shell Programming and Scripting

FTP script for sending a file from one unix directory to another unix server director

Hi, My local server is :/usr/abcd/ Remote server is :/Usr/host/test/ I want to send files from local unix directory(All files starting with O_999) to remote host unix directory. Can any body give me the Unix Shell script to do this. One more doubt: Shall we need to change the file... (1 Reply)
Discussion started by: raja_1234
1 Replies
Login or Register to Ask a Question