nixCraft Linux Forum

nixCraft

Linux Tech Support Forum

contents of files

This is a discussion on contents of files within the Shell scripting forums, part of the Development/Scripting category; Hello friends, I have a small question which is as follows. " I have a folder of 100 .txt files ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Register FAQ Members List Calendar Forgotten your password? Mark Forums Read
  #1 (permalink)  
Old 08-30-2005, 10:34 AM
guest
Guest
 
Posts: n/a
Default contents of files

Hello friends,
I have a small question which is as follows. " I have a folder of 100 .txt files all of which have 26 lines of text data (not useful to me). From the 27th line, i have 2 columns of decimal data. Imagine it to be like
0.400 13.2345
0.402 13.6301
0.410 12.8842
........ ....
....
....(220 lines like thi
Now all i need is the second column (13.2345,13.6301,.etc.) from all these .txt files. All second column data from all the .txt files should be copied into a new file "sequentially"." How can we do this?
As for my attempt, i have tried doing a command line instruction like this
sed -n '27,$s/ *[^ ]* *\(.*\)/\1/w {o/p file}' {input .txt file}
However this doesnt work when i use a for loop in a shell script. I have also used the >> {output file name} option, but it isnt working.
I would be glad if anyone can give any suggestions.
thank you.
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-30-2005, 04:19 PM
monk's Avatar
Senior Member
User
 
Join Date: Jan 2005
Location: Tibet
My distro: Debian GNU/Linux
Posts: 482
Rep Power: 5
monk will become famous soon enough monk will become famous soon enough
Default Re: contents of files

Well not sure about sed code, but following should work
Code:
files="*.txt"
OFILE="/tmp/output"
# 220-26 so start from 27th line
LINES=193 
>$OFILE
for f in $files
do
tail -$LINES $f | awk '{ print $2 }' >> $OFILE
done
Open file $OFILE and see output ;test it and lemme know!
Reply With Quote
  #3 (permalink)  
Old 08-31-2005, 12:39 AM
guest
Guest
 
Posts: n/a
Default contents of file

thanx monk,
that worked. I found out the output file has only column 2 of the data. but then the output file was far bigger than i expected. I assume it is because the data is written on a new line everytime. Like C file operations is it possible to but data next to each other? so that the file size will be exactly the no. of data multiplied by the bytes( here it would be 193 * 100 files * 8 bytes for long data). I want the data to be placed next to each other.
thanx again for that reply.......i didnt use awk i tried cut -f2 filename which didnt work out!
Reply With Quote
  #4 (permalink)  
Old 08-31-2005, 01:34 AM
monk's Avatar
Senior Member
User
 
Join Date: Jan 2005
Location: Tibet
My distro: Debian GNU/Linux
Posts: 482
Rep Power: 5
monk will become famous soon enough monk will become famous soon enough
Default

Replace line
Code:
tail -$LINES $f | awk '{ print $2 }' >> $OFILE
with
Code:
DATA="$(tail -$LINES $f | awk '{ print $2 }')"
And then use $DATA as per your need.
Reply With Quote
  #5 (permalink)  
Old 09-01-2005, 12:02 AM
guest
Guest
 
Posts: n/a
Default

monk,
perhaps the double quotes dont work. When i try to echo $DATA, all it displays is the same command "tail +LINES.............".
I understand that what u meant is to copy all the data into the variable DATA and then print it to a new file? am i right?
I have tried putting the right hand side within ` ` instead of " " but that didnt work out too.
Reply With Quote
  #6 (permalink)  
Old 09-01-2005, 12:23 AM
tom tom is offline
Contributors
User
 
Join Date: Jun 2005
Location: London, UK
Posts: 213
Rep Power: 4
tom is on a distinguished road
Default

I'm not expert in scripting but I think you need to get your data in awk script/command itself. Just thought...
Reply With Quote
  #7 (permalink)  
Old 09-01-2005, 12:44 AM
monk's Avatar
Senior Member
User
 
Join Date: Jan 2005
Location: Tibet
My distro: Debian GNU/Linux
Posts: 482
Rep Power: 5
monk will become famous soon enough monk will become famous soon enough
Default

Try following
Code:
DATA="$DATA $(tail -$LINES $f | awk '{ print $2 }')"
So it should look like as follows:

Code:
files="*.txt"
OFILE="/tmp/output"
# 220-26 so start from 27th line
LINES=193
>$OFILE
DATA=""
for f in $files
do
DATA="$DATA $(tail -$LINES $f | awk '{ print $2 }')" 
done
Lemme know...
Reply With Quote
  #8 (permalink)  
Old 09-01-2005, 02:41 AM
guest
Guest
 
Posts: n/a
Default

I tried doing that....sorry for the delay .....being new to shell script........i am taking time to follow it.
I used the above code. I think it works though using echo $DATA gives me a stream of values

Also i am trying to output DATA to a new file which is not happening. I used ( DATA >>OFILE) but that gives me an error.
Reply With Quote
  #9 (permalink)  
Old 09-01-2005, 11:40 AM
rockdalinux's Avatar
Contributors
User
 
Join Date: May 2005
Location: Bangalore
My distro: RHEL, HP-UX, Solaris, FreeBSD, Ubuntu
Posts: 581
Rep Power: 7
rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough
Default

Add echo $DATA at the end of loop:
Code:
files="*.txt"
OFILE="/tmp/output"
# 220-26 so start from 27th line
LINES=193
>$OFILE
DATA=""
for f in $files
do
DATA="$DATA $(tail -$LINES $f | awk '{ print $2 }')"
done 

echo $DATA>$OFILE
__________________
Rocky Jr.
You may have my body & soul, but you will never touch my pride!

If you have knowledge, let others light their candles at it.

Certified to work on HP-UX / Sun Solaris / RedHat
Reply With Quote
  #10 (permalink)  
Old 09-02-2005, 01:09 AM
guest
Guest
 
Posts: n/a
Default

my apologies for not telling u that i did echo ( the last line) to OFILE.....my head was somewhere else when i replied to u. even when i did that i viewed the data (less /tmp/output) the data looked like this
2.3061^M8.6088^M...................

the thing was the file was still the same size as the previous output file which had newline in between..............
Right now what i am doing is to write a C program ( because i need to write it for some other application) and reading a floating pt. (or long for 8 byte and also a character so that whatever this ^M is i can get rid of.
What do u say?
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads

Thread Thread Starter Forum Replies Last Post
UNIX list a backup tape contents chiku Getting started tutorials 0 04-27-2007 05:46 PM
Shell script for automatic conversion of files in tar files kasimani Shell scripting 2 02-08-2007 04:45 PM


All times are GMT +5.5. The time now is 05:24 PM.


Powered by vBulletin® Version 3.7.4 - Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36