Grab from the file in one command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grab from the file in one command
# 1  
Old 01-27-2010
Grab from the file in one command

Dear All,

I have a file in which there are 54 fields, i want to grab the all the lines and send to a new file where filed18 has lenght greater than 14. How can i do it without if condition and faster way:

currently i am reading file line by line and comparing the length

Code:
read fileLine || break
        oldDate=`echo $fileLine | cut -f18 -d "|" `
        if [ "${#oldDate}" -gt 14 ]
        then
                `echo $fileLine >> $newFileName`
        fi

can anyone suggest better and improved way. Thanks

Last edited by Scott; 01-27-2010 at 04:50 AM.. Reason: Please use code tags
# 2  
Old 01-27-2010
Code:
awk 'length($18) > 14' input_file > new_file

# 3  
Old 01-27-2010
Quote:
Originally Posted by scottn
Code:
awk 'length($18) > 14' input_file > new_file

If you read the code which bilalghazi provided, there is | to be used in cut.

So the code should be fixed by:

Code:
awk -F "|" 'length($18) > 14' input_file > new_file

# 4  
Old 01-27-2010
Quote:
Originally Posted by rdcwayx
If you read the code which bilalghazi provided, there is | to be used in cut.

So the code should be fixed by:

Code:
awk -F "|" 'length($18) > 14' input_file > new_file

Good spot... thanks Smilie
# 5  
Old 01-27-2010
Thanks a lot, it resolved my problem. Smilie

---------- Post updated at 05:36 PM ---------- Previous update was at 05:35 PM ----------

is it possible that i replace the value of field18 with a variable value and then save the whole line in the new file, only if the length of field18 is greater than 14.
# 6  
Old 01-27-2010
Code:

awk -F "|" 'length($F) > L' F=18 L=14 input_file > new_file

# 7  
Old 01-27-2010
i think i was not clear enough to explain my problem, the problem is that i have one variable which has some value say value1 now i want to check each line of file, if the field18 is greater than 14 than replace whatever in the field18 with value1. I hope i am clear this time. Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk remove/grab lines from file with pattern from other file

Sorry for the weird title but i have the following problem. We have several files which have between 10000 and about 500000 lines in them. From these files we want to remove lines which contain a pattern which is located in another file (around 20000 lines, all EAN codes). We also want to get... (28 Replies)
Discussion started by: SDohmen
28 Replies

2. Shell Programming and Scripting

Read file, grab ip with fail2ban

Solved with iptables. Many thanks... Hello, Objective: What I would like to accomplish is : - To read file1 line by line and search each word in file2. - To grab corresponding ip addresses found in file2 - To send related ip addresses to fail2ban (not iptables) By this way, when I... (5 Replies)
Discussion started by: baris35
5 Replies

3. Shell Programming and Scripting

How to grab a block of data in a file with repeating pattern?

I need to send email to receipient in each block of data in a file which has the sender address under TO and just send that block of data where it ends as COMPANY. I tried to work this out by getting line numbers of the string HELLO but unable to grab the next block of data to send the next... (5 Replies)
Discussion started by: loggedout
5 Replies

4. Shell Programming and Scripting

Grab data within a table in a long log file.

in my file which is a rather long log file it contains many text and tables and there is one table with 15 columns and I am interested to read in the value in column6 and its corresponding value in column2. Trouble is I do not know how to script it as the line number various between different log... (8 Replies)
Discussion started by: piynik
8 Replies

5. Shell Programming and Scripting

Grab from file with sed

Hello All I have a file with this type of records: =LDR 01157nas a22003011a 4500 =001 vtls000000013 =003 VRT =005 20111020150800.0 =008 100128c19699999sp\a|||||\||||0\\\||spa| =037 \\$a1327$i090$j090$k03 =039 ... (14 Replies)
Discussion started by: ldiaz2106
14 Replies

6. Shell Programming and Scripting

Grab 2 pieces of data within a file

I am a newbie and what I have is a captured file of content. I want to be able to grab 2 pieces of data, multiple times and print them to the screen. DataFile owner: locke user: fun data size: 60 location: Anaheim owner: david user: work data size: 80 location: Orange my script... (2 Replies)
Discussion started by: greglocke
2 Replies

7. Shell Programming and Scripting

Help me grab a passage out of this text file?

Hey guys, I need a command that grabs only this part of the .txt file (that is attached), and outputs it to another .txt file with only these contents below. Thanks in advanced brothers: Disk: Local Disk (C:), NTFS Disk Defragmentation Summary Disk Size 230.85 GB Free Space Size... (4 Replies)
Discussion started by: aabbasi
4 Replies

8. Windows & DOS: Issues & Discussions

DOS script to grab the first file in a dir and rename it

:confused: Hello, Is there any way to use the dir command / some DOS Script to select only first file of similar pattern of files in a direcotory and rename it for example, one directory has 5 files abc_1005.txt abc_5256.txt abc_2001.txt abc_2003.txt abc_3006.txt by use script I would... (2 Replies)
Discussion started by: raghav525
2 Replies

9. UNIX for Dummies Questions & Answers

search and grab data from a huge file

folks, In my working directory, there a multiple large files which only contain one line in the file. The line is too long to use "grep", so any help? For example, if I want to find if these files contain a string like "93849", what command I should use? Also, there is oder_id number... (1 Reply)
Discussion started by: ting123
1 Replies

10. UNIX for Dummies Questions & Answers

How to Grab the latest file

I have been trying to use the find command to grab the latest file in a directory and move it to another area. I can't seem to get only that file, I end up getting everything for the day. Any ideas? Thank you (1 Reply)
Discussion started by: n9ninchd
1 Replies
Login or Register to Ask a Question