Need help on getting the first few characters of a file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help on getting the first few characters of a file.
# 1  
Old 06-17-2010
Need help on getting the first few characters of a file.

Friends ,
I need some help in writing a shell script to get the first few characters from a file for all the lines in that file.

Here is sample data inthe file.
Code:
9 JACOBS 0175 
10 VENDOR_0175 
11 JACOBS 0175 
100 0175

I want the ouput as
Code:
9,10,11,100

It doesn't have any fixed length,the only thing I can say is the first I need all the caracters until it first hits 2 spaces from each line in the file.

Last edited by Scott; 06-17-2010 at 01:57 PM.. Reason: Code tags, please...
# 2  
Old 06-17-2010
Code:
$ cut -f1 -d\  file1 | paste -sd, -
9,10,11,100

This User Gave Thanks to Scott For This Post:
# 3  
Old 06-17-2010
Code:
#!/bin/ksh or dash
# first
while read one xstr
do
     printf "%s," $one
done
echo

Using example:
Code:
chmod a+rx first
cat somefile | ./first  > result.txt

This User Gave Thanks to kshji For This Post:
# 4  
Old 06-17-2010
Quote:
Originally Posted by scottn
Code:
$ cut -f1 -d\  file1 | paste -sd, -
9,10,11,100

Scott ,
It worked awesome.
Thanks you so much .
But, can you please explain what exactly the syntax means?
# 5  
Old 06-17-2010
Quote:
Originally Posted by srini02
Scott ,
It worked awesome.
Thanks you so much .
But, can you please explain what exactly the syntax means?
Hi.

Perhaps this is a bit clearer:
Code:
cut -f1 -d" " file1 | paste -sd, -

It "cuts" the first field (-f1) from the file file1 using a delimeter of space (-d" "), then pipes this to the paste command which concatenates (-s) the lines together with a delimeter of a comma (-d,) using the file - (standard input) (i.e. the output of the cut command).
This User Gave Thanks to Scott For This Post:
# 6  
Old 06-17-2010
Code:
$
$ cat f6
9 JACOBS 0175
10 VENDOR_0175
11 JACOBS 0175
100 0175
$
$ awk '{x = x","$1} END{print substr(x,2)}' f6
9,10,11,100
$

tyler_durden
This User Gave Thanks to durden_tyler For This Post:
# 7  
Old 06-20-2010
suppose ' abc ' is the file name containing all the data then

Code:
cut -d" " -f1 abc |n paste -s d","


Last edited by radoulov; 06-20-2010 at 11:40 AM.. Reason: Please use code tags!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Outputting characters after a given string and reporting the characters in the row below --sed

I have this fastq file: @M04961:22:000000000-B5VGJ:1:1101:9280:7106 1:N:0:86 GGGGGGGGGGGGCATGAAAACATACAAACCGTCTTTCCAGAAATTGTTCCAAGTATCGGCAACAGCTTTATCAATACCATGAAAAATATCAACCACACCA +test-1 GGGGGGGGGGGGGGGGGCCGGGGGFF,EDFFGEDFG,@DGGCGGEGGG7DCGGGF68CGFFFGGGG@CGDGFFDFEFEFF:30CGAFFDFEFF8CAF;;8... (10 Replies)
Discussion started by: Xterra
10 Replies

2. Linux

File conversion and removing special characters from a file in Linux

I have a .CSV file when I check for the special characters in the file using the command cat -vet filename.csv, i get very lengthy lines with "^@", "^I^@" and "^@^M" characters in between each alphabet in all of the records. Using the code below file filename.csv I get the output as I have a... (2 Replies)
Discussion started by: dhruuv369
2 Replies

3. Shell Programming and Scripting

awk script to count characters in file 1 in file 2

I need a scripting AWK to compare 2 files. file 1 and 2 are list of keywords 1 is a b c d 2 is aa aaa b bb ccc d I want the AWK script to give us the number of times every keyword in file 1 occurs in file 2. output should be a 2 (7 Replies)
Discussion started by: anhtt
7 Replies

4. Shell Programming and Scripting

sed replacing specific characters and control characters by escaping

sed -e "s// /g" old.txt > new.txt While I do know some control characters need to be escaped, can normal characters also be escaped and still work the same way? Basically I do not know all control characters that have a special meaning, for example, ?, ., % have a meaning and have to be escaped... (11 Replies)
Discussion started by: ijustneeda
11 Replies

5. Shell Programming and Scripting

File contains ^M characters

Hello, I tried with this command,it's working fine,file contains huge data it's not removing the control+M character dos2unix test1.txt test2.txt Please help me,is there any other option. :wall: Thanks, Murali ---------- Post updated at 06:41 AM ---------- Previous update... (6 Replies)
Discussion started by: muralikri
6 Replies

6. Shell Programming and Scripting

Unix Script file to Append Characters before rows in file.

Hi Experts, I am working on HP-UX. I am new to shell scripting. I would like to have a shell script which will prefix: 1. "H|" before first row of my file and, 2. "T" for all other rows of the file. For Example - File before running the script 20100430|4123451810|218.50|TC 20100430 ... (4 Replies)
Discussion started by: phani333
4 Replies

7. Shell Programming and Scripting

Trying to remove '^M' characters from a file.

Hi guys, Hope you are all well. This is a line of data from a csv file. I have used vi and set the 'set list' option to display the trailing $ character. "01","Grocery","01006","eat Fish & Spreads"$ I have tried the following commands, but neither of them appear to be working? 1) tr... (13 Replies)
Discussion started by: Krispy
13 Replies

8. Shell Programming and Scripting

\r characters in the file

hi I copied a file to unix box from windows. When i run this file it throws error on every command written in that file. The reason is that the file contains some \r charecters that came into picture while copying the file. This could be viewed by using od -c command. My question is that... (2 Replies)
Discussion started by: infyanurag
2 Replies

9. UNIX for Dummies Questions & Answers

Rename file based on first 3 characters of data in file

I'm looking to determine if I can use a grep command to read file and rename the file based on the first 3 characters of the data in the file. An example is: Read FileA If the first 3 positions of the data in the file are "ITP", then rename the file as FileA_ITP, else if the first 3... (3 Replies)
Discussion started by: jchappel
3 Replies

10. UNIX for Dummies Questions & Answers

grepping the first 3 characters from a file

Hi I was wondering if it's possible to use a command to get the first 3 characters of a line in a text file, I tried grep but it returns the whole line but I am only interested in the first 3 characters. Is this possible with grep or I need any other command? Also is it possible deleting from... (2 Replies)
Discussion started by: g-e-n-o
2 Replies
Login or Register to Ask a Question