Help to write a script for the below requirement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help to write a script for the below requirement
# 1  
Old 08-01-2013
Help to write a script for the below requirement

Hi,

I am not so familiar with shell Script but I have a task in hand. So, here it goes:

There is a file details.txt in the server where the below details are stored:
Code:
Name Country D  M    Acc.No.
sameer India  30 july  sscc-errttt-q
random US     20 july    pecc-ttt4-s
Deb      India  31 july  ssee-tttttt-s
Amit     US     20 july    recc-ttt7-s
Sharad  India 31 july   ssee-tttttt-s

Now, the 5th column,i.e. Acc.No. ideally should have a length of 13 bytes where as in 2 cases (I am only showing 2, but there are many in reality) it is of 11 bytes. So I want to write a script to take out those lines (the complete line) which are having length as 11 bytes in the Acc.No. column and direct them to a file. I am in a fix as I dont know how to achieve it. Kindly provide me the solution. So, the o/p file shuld have entry like:
Code:
Name Country D   M    Acc.No.
random US     20 july    pecc-ttt4-s
Amit     US     20 july    recc-ttt7-s


Last edited by Franklin52; 08-02-2013 at 03:19 AM.. Reason: Please use code tags
# 2  
Old 08-01-2013
try:
Code:
awk 'NR==1 || length($5)!=13' details.txt > new_file.txt

This User Gave Thanks to rdrtx1 For This Post:
# 3  
Old 08-01-2013
Another approach using bash:
Code:
#!/bin/bash

while read nm cn dt mm acc
do
        [ "$nm" = "Name" ] && printf "%s\t%s\t%s\t%s\t%s\n" "${nm}" "${cn}" "${dt}" "${mm}" "${acc}"
        [ "${#acc}" -eq 11 ] && printf "%s\t%s\t%s\t%s\t%s\n" "${nm}" "${cn}" "${dt}" "${mm}" "${acc}"

done < file

Output:
Code:
Name    Country D       M       Acc.No.
random  US      20      july    pecc-ttt4-s
Amit    US      20      july    recc-ttt7-s

This User Gave Thanks to Yoda For This Post:
# 4  
Old 08-03-2013
Thank u so much for your help.. the second one worked smoothly. for teh 1st solution, it goes into an infinite loop saying commannot found (the awk command is providing the result though when used explicitly). I am using while loop. Can you help?
~Neel
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Script requirement

Please help in creating script for below requirement. I will be running 1 command and will get below entries in a text file say file44.txt ******************************* DFDL1005 06:30 00:05 ABFL2003 10/22 01:10 CFTL1256 10/24 00:10 10/25 09:20 PM ******************************** .... (3 Replies)
Discussion started by: Vinay_3308
3 Replies

2. Shell Programming and Scripting

Writing if condition in shell script and failing to do requirement

Hi, I am trying to edit the values in a file. For example i am trying to edit the value of "ABC" in a file by executing shell script. Please Note that ABC value can be there mulitple times or it may not be there in the file Conditions for it is 1. If ABC is less than 123 then it should... (14 Replies)
Discussion started by: darling
14 Replies

3. Shell Programming and Scripting

How to write bash shell script for mentioned requirement?

Hi All, I am unable to write the script for the below requirement. Requirement: Main table dir_ancillary table contain three column "dir_ancillary.table_name"," dir_ancillary.text_file_name ", "dir_ancillary.Include" . This dir_ancillary contain undefined tables in the column... (2 Replies)
Discussion started by: Vineeta Nigam
2 Replies

4. Shell Programming and Scripting

Looping requirement

Hi all, I have little working knowledge in unix shell scripting. I have a requirement where i need to pull out some data in between the strings in the file. Input: TEST a a c f d TEST f e g g TEST Output: (7 Replies)
Discussion started by: satyasrin82
7 Replies

5. IP Networking

read/write,write/write lock with smbclient fails

Hi, We have smb client running on two of the linux boxes and smb server on another linux system. During a backup operation which uses smb, read of a file was allowed while write to the same file was going on.Also simultaneous writes to the same file were allowed.Following are the settings in the... (1 Reply)
Discussion started by: swatidas11
1 Replies

6. Shell Programming and Scripting

Awk script with a urgent requirement

The Problem content is pin (AND1) { dir : output; cap : hot; tran: slew; } need to write a awk script which can search the Pin AND1 and then tran of the Group pin(AND1) and then insert the code from the file "insert_code" . insert_code file contents . It is a Big file the script should... (3 Replies)
Discussion started by: kshitij
3 Replies

7. Shell Programming and Scripting

Requirement

I am trying to script and came up with a conclusion that I need a do while loop in my statement. I am stuck with the do while syntax. I need to use it alongwith the if then else statement. Can I use it is a big question? I actually need to get all the files that are there from within run_dt to... (1 Reply)
Discussion started by: aronmelon
1 Replies

8. UNIX for Dummies Questions & Answers

Should I write a PERL Script or Shell Script?

Hello, I have done some BASIC shell scripting/PERL scripting before so I am familiar with the languages. I am not really sure which one would lend itself better to the application I have to write. I am required to scan the message logs for possible break in attempts. If I use shell scripting... (2 Replies)
Discussion started by: mojoman
2 Replies

9. Shell Programming and Scripting

Required Shell script for My requirement

Hi All, I joined today in this forum to have all of your help. I have a Big requirement, pls. help me to resolve. I'm using HP-UX 11.23. I need a shell script for the following requirement. I have a file (nodes.txt) that contains 1000 nodes. I'm running the following command:... (3 Replies)
Discussion started by: ntgobinath
3 Replies
Login or Register to Ask a Question