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 ...
|
|||||||
| Register | FAQ | Members List | Calendar | Forgotten your password? | Mark Forums Read |
|
|||
|
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. |
| Sponsored Links | ||
|
|
|
|||
|
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! |
|
|||
|
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. |
|
||||
|
Try following
Code:
DATA="$DATA $(tail -$LINES $f | awk '{ print $2 }')"
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
|
|
|||
|
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. |
|
||||
|
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 |
|
|||
|
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? |
![]() |
| Bookmarks |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) |
|
| Thread Tools | |
| Display Modes | |
|
|
|
||||
| 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 |