How to cut first line only from a text near a specific column without cutting a word


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to cut first line only from a text near a specific column without cutting a word
# 1  
Old 05-14-2009
How to cut first line only from a text near a specific column without cutting a word

First I have to say thank you to this community and this forum. You helped me very much builing several useful scripts.

Now, I can't get a solution the following problem, I'm stuck somehow. Maybe someone has an idea.

In short, I dump a site via lynx and pipe the output in a file. I need to cut - the first line only -near the 15th column, by piping aswell.

The Information I need is always in this area 1 - 15 col.

A word or chars which are together should not be cut at this pont. So the cut should take place after the next blank (space).

example
123456789012345678901234567890 <-- column
balah bb 213112312 12321 <-- random information


balah bb 213112312 <-the result should be this

I tried several approaches with fold and awk and cut, but the results were not ok.

the information below the first line must stay untouched and sould not be wrapped aswell. Thats the problem I have.
# 2  
Old 05-14-2009
i don't understand.. show an actual sample of the file and output desired.
# 3  
Old 05-14-2009
this one:

balah bb 213112312 12321 balah bb 213112312 12321 balah bb 213112312 12321
ah bb 213112312 12321 balah bb 213112312 12321 balah bb 213112312 123
ah bb 213112312 12321 balah bb 213112312 12321 balah bb 213112312 123
b 213112312 12321 balah bb 213112312 12321 balah bb 213112312 12321
ah bb 21311b 213112312 12321 balah bb 213112312 12321 balah bb 213112312 12321
ah bb 21311b 21b 213112312 12321 balah bb 213112312 12321 balah bb 213112312 12321
ah bb 21311b 213112b 213112312 12321 balah bb 213112312 12321 balah bb 213112312 12321
ah bb 21311

should look like this:

balah bb 213112312
ah bb 213112312 12321 balah bb 213112312 12321 balah bb 213112312 123
ah bb 213112312 12321 balah bb 213112312 12321 balah bb 213112312 123
b 213112312 12321 balah bb 213112312 12321 balah bb 213112312 12321
ah bb 21311b 213112312 12321 balah bb 213112312 12321 balah bb 213112312 12321
ah bb 21311b 21b 213112312 12321 balah bb 213112312 12321 balah bb 213112312 12321
ah bb 21311b 213112b 213112312 12321 balah bb 213112312 12321 balah bb 213112312 12321
ah bb 21311

the data is random and can be anything...
# 4  
Old 05-15-2009
ok so you are only processing the first line. If you have Python,
Code:
#!/usr/bin/env python
f=open("file")
firstline=f.readline().strip()[:19]
print firstline
for line in f: print line.strip()
f.close()

output:
Code:
# ./test.py
balah bb 213112312
ah bb 213112312 12321 balah bb 213112312 12321 balah bb 213112312 123
ah bb 213112312 12321 balah bb 213112312 12321 balah bb 213112312 123
b 213112312 12321 balah bb 213112312 12321 balah bb 213112312 12321
ah bb 21311b 213112312 12321 balah bb 213112312 12321 balah bb 213112312 12321
ah bb 21311b 21b 213112312 12321 balah bb 213112312 12321 balah bb 213112312 12321
ah bb 21311b 213112b 213112312 12321 balah bb 213112312 12321 balah bb 213112312 12321
ah bb 21311

else:
Code:
# awk 'NR==1{print substr($0,1,19);next}1' file

# 5  
Old 05-15-2009
wow, thanks for the fast answer.

# awk 'NR==1{print substr($0,1,19);next}1' file

This cuts the line correct if the blank is at column 19. What if the line is random so that the output for the 2 examples should look like this, a cut near column 15:

ah bb 213112312 12321 balah bb 213112312 12321 balah bb 213112312 123
ah bb 213112312


b 213112312 12321 balah bb 213112312 12321 balah bb 213112312 12321
b 213112312 12321 or
b 213112312
# 6  
Old 05-15-2009
Maybe something like this:

Code:
$
$ cat input.txt
balah bb 213112312 12321 balah bb 213112312 12321 balah bb 213112312 12321
ah bb 213112312 12321 balah bb 213112312 12321 balah bb 213112312 123
ah bb 213112312 12321 balah bb 213112312 12321 balah bb 213112312 123
b 213112312 12321 balah bb 213112312 12321 balah bb 213112312 12321
ah bb 21311b 213112312 12321 balah bb 213112312 12321 balah bb 213112312 12321
ah bb 21311b 21b 213112312 12321 balah bb 213112312 12321 balah bb 213112312 12321
ah bb 21311b 213112b 213112312 12321 balah bb 213112312 12321 balah bb 213112312 12321
ah bb 21311
$
$ perl -ne '{if ($. eq 1){print substr($_,0,index($_," ",14))."\n"} else {print}}' input.txt
balah bb 213112312
ah bb 213112312 12321 balah bb 213112312 12321 balah bb 213112312 123
ah bb 213112312 12321 balah bb 213112312 12321 balah bb 213112312 123
b 213112312 12321 balah bb 213112312 12321 balah bb 213112312 12321
ah bb 21311b 213112312 12321 balah bb 213112312 12321 balah bb 213112312 12321
ah bb 21311b 21b 213112312 12321 balah bb 213112312 12321 balah bb 213112312 12321
ah bb 21311b 213112b 213112312 12321 balah bb 213112312 12321 balah bb 213112312 12321
ah bb 21311
$
$

tyler_durden
# 7  
Old 05-15-2009
Quote:
Originally Posted by lowmaster
...What if the line is random so that the output for the 2 examples should look like this, a cut near column 15:

ah bb 213112312 12321 balah bb 213112312 12321 balah bb 213112312 123
ah bb 213112312

b 213112312 12321 balah bb 213112312 12321 balah bb 213112312 12321
b 213112312 12321 or
b 213112312
Code:
$
$ cat input1.txt
ah bb 213112312 12321 balah bb 213112312 12321 balah bb 213112312 123
$
$ perl -ne '{if ($. eq 1){print substr($_,0,index($_," ",14))."\n"} else {print}}' input1.txt
ah bb 213112312
$
$
$ cat input2.txt
b 213112312 12321 balah bb 213112312 12321 balah bb 213112312 12321
$
$
$ perl -ne '{if ($. eq 1){print substr($_,0,index($_," ",14))."\n"} else {print}}' input2.txt
b 213112312 12321
$
$

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Cut specific column from 2 file and merge

Hi ALL, I have two file. I need to combine these two file based on a layout. I used the below code and able to extract the record. But now able to insert that to a 3'rd file in between the extract FILE 1 CAID NUMBER 1-20 TID NUMBER 21-22 LABEL CHAR 23-44 BASE 45-60... (5 Replies)
Discussion started by: arunkumar_mca
5 Replies

2. Shell Programming and Scripting

Overwrite specific column in xml file with the specific column from adjacent line

I have an xml file dumped from rrd file, that I want to "patch" so the xml file doesn't contain any blank hole in the resulting graph of the rrd file. Here is the file. <!-- 2015-10-12 14:00:00 WIB / 1444633200 --> <row><v> 4.0419731265e+07 </v><v> 4.5045912770e+06... (2 Replies)
Discussion started by: rk4k
2 Replies

3. Shell Programming and Scripting

How to print multiple specific column after a specific word?

Hello.... Pls help me (and sorry my english) :) So I have a file (test.txt) with 1 long line.... for example: isgc jsfh udgf osff 8462 error iwzr 653 idchisfb isfbisfb sihfjfeb isfhsi gcz eifh How to print after the "error" word the 2nd 4th 5th and 7th word?? output well be: 653 isfbisfb... (2 Replies)
Discussion started by: marvinandco
2 Replies

4. UNIX for Dummies Questions & Answers

How to cut from a text file based on value of a specific column?

Hi, I have a tab delimited text file from which I want to cut out specific columns. If the second column equals one, I want to cut out columns 1 and 5 and 6. If the second column equals two, I want to cut out columns 1 and 5 and 7. How do I go about doing that? Thanks! (4 Replies)
Discussion started by: evelibertine
4 Replies

5. UNIX for Dummies Questions & Answers

How to print line starts with specific word and contains specific word using sed?

Hi, I have gone through may posts and dint find exact solution for my requirement. I have file which consists below data and same file have lot of other data. <MAPPING DESCRIPTION ='' ISVALID ='YES' NAME='m_TASK_UPDATE' OBJECTVERSION ='1'> <MAPPING DESCRIPTION ='' ISVALID ='NO'... (11 Replies)
Discussion started by: tmalik79
11 Replies

6. Shell Programming and Scripting

Cutting out text from specific portion on filename

Hi, how do I go about cutting out the first numeric characters after the word "access"? access1005101228.merged-00.15.17.86.d8.b8.log.gz (16 Replies)
Discussion started by: GermanJulian
16 Replies

7. Shell Programming and Scripting

Cutting specific line of a file by checking condition

testfile.csv 0","1125209",,"689202CBx18888",,"49",,,"NONMC",,,,,"01112010",,,,,,,"MTM- "1","",,"689202ABx19005",,"49",,,"NONMC",,,,,"01072010",,,,,,,"MTM- testfile.csv looks like above format if the second column is null then get 23rd column and store in a different varible .. add all the... (1 Reply)
Discussion started by: mgant
1 Replies

8. Shell Programming and Scripting

Help need to cut the first word of a line in text file

Hi All, I would like help with a script which can get rid of the first work of all lines in text file. File 1 The name is Scott. Output : name is Scott ---------- Post updated at 02:38 PM ---------- Previous update was at 02:37 PM ---------- Hi ALL There is typo error in... (3 Replies)
Discussion started by: bubbly
3 Replies

9. Shell Programming and Scripting

How to replace a specific word in specific column?

Hi My orginal file is like (100s of lines) id host ip location remarks 1 host1 ip1 - xxx 2 host2 ip2 - xxx 3 host3 ip3 - xxx -- -- 9 host9 ip9 - xxx I have a ref file like host1 location1 host2 location2 host3 location3 -- --... (6 Replies)
Discussion started by: ./hari.sh
6 Replies

10. UNIX for Advanced & Expert Users

cut a specific value from a column

Dear Friends, I have an output like this: 7072;0;7072901 7072;1001;7072902 7072;101;7072903 7072;102;7072904 7072;1101;7072905 7072;1301;7072906 7072;1401;7072907 7072;162;7072908 7072;1;7072909 and I need to print the value in the column 3 , row number 1. which is 7072901 only.... (2 Replies)
Discussion started by: sfaqih
2 Replies
Login or Register to Ask a Question