save only the first letter from lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting save only the first letter from lines
# 1  
Old 01-10-2011
Java save only the first letter from lines

the title is self explanatory

I assume awk and sed existence to do the job but i'm really not good in pattern space comprehension to make it so

I wanna pick the first letter of each line in a file, what's the way ?

Thanks a lot
# 2  
Old 01-10-2011
Code:
while read line; do echo ${line:0:1} >>output.txt; done<input.txt

This User Gave Thanks to EAGL€ For This Post:
# 3  
Old 01-10-2011
Code:
 sed -r 's/^(.).*/\1/' file

This User Gave Thanks to JavaHater For This Post:
# 4  
Old 01-10-2011
Code:
$ cat infile
begin
   two
   4-Fun
end
 
$ cut -c1 infile
b
 
e
 
$ sed 's/^[ \t]*\(.\).*/\1/' infile
b
t
4
e
 
$ sed 's/^[^a-zA-Z]*\([a-zA-Z]\).*/\1/' infile
b
t
F
e


Last edited by Chubler_XL; 01-10-2011 at 09:23 PM.. Reason: Added some more examples
This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 01-10-2011
Assuming it is the first LETTER you are looking for:
Code:
$ sed 's/^[^[:alpha:]]*\([[:alpha:]]\).*/\1/'  infile
b
t
F
e
$

Should work for any locale.
This User Gave Thanks to fpmurphy For This Post:
# 6  
Old 01-10-2011
Code:
   sed -r 's/^(.).*/\1/' file  
sed -rn 's/^[[:space:]]*([[:alpha:]]).*/\1/p' file

This User Gave Thanks to JavaHater For This Post:
# 7  
Old 01-11-2011
print the first real letter, not space or tab.
Code:
awk '{print substr($1,1,1)}' infile

print the first char.

Code:
cut -c 1 < infile

This User Gave Thanks to rdcwayx For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace specific letter in a file by other letter

Good afternoon all, I want to ask how to change some letter in my file with other letter in spesific line eg. data.txt 1 1 1 0 0 0 0 for example i want to change the 4th line with character 1. How could I do it by SED or AWK. I have tried to run this code but actually did not... (3 Replies)
Discussion started by: weslyarfan
3 Replies

2. Shell Programming and Scripting

Remove duplicate lines, sort it and save it as file itself

Hi, all I have a csv file that I would like to remove duplicate lines based on 1st field and sort them by the 1st field. If there are more than 1 line which is same on the 1st field, I want to keep the first line of them and remove the rest. I think I have to use uniq or something, but I still... (8 Replies)
Discussion started by: refrain
8 Replies

3. Shell Programming and Scripting

Multiplying lines of file that contain certain letter by value

I am trying to remove the last letter in a file and then multiply each line (which contained this letter) by 500. This is what I have: 1499998A 1222222A 1325804A 1254556 1235 9998 777 cat /tmp/listzz |gawk '{print $4}'|gawk '{gsub(//, ""); print } This removes the A... (1 Reply)
Discussion started by: newbie2010
1 Replies

4. Shell Programming and Scripting

Save value from output of Corestat and save in a list for each core

I am trying to modify the "corestat v1.1" code which is in Perl.The typical output of this code is below: Core Utilization CoreId %Usr %Sys %Total ------ ----- ----- ------ 5 4.91 0.01 4.92 6 0.06 ... (0 Replies)
Discussion started by: Zam_1234
0 Replies

5. Shell Programming and Scripting

loop through lines and save into separate files

I have two files: file-gene_families.txt that contains 30,000 rows of 30 columns. Column 1 is the ID column and contains the Col1 Col2 Col3 ... One gene-encoded CBPs ABC 111 ... One gene-encoded CBPs ABC 222 ... One gene-encoded CBPs ABC 212 ... Two gene encoded CBPs EFC... (7 Replies)
Discussion started by: yifangt
7 Replies

6. Shell Programming and Scripting

Pulling x number of lines from one file and save into another

Hi, I have a log file (updates.log), and I want to hunt the file for any errors. Here's an example of the log file: SQL> update <table1> set <value1> = '*****'; update <table1> set <value1> = '*****' * ERROR at line 1: ORA-00942: table or view does not exist Elapsed:... (4 Replies)
Discussion started by: dbchud
4 Replies

7. UNIX for Dummies Questions & Answers

sort lines in different files based on the starting letter

Hi ,, i have the below file... D 2342135 B 214236 C argjlksd V lskjrghaklsr C slkrgj B sdg4tsd E aslkgjlkasg i want to sort the lines into different files based on the starting letter of the line. so that i have different files for lines starting with a letter. thanks (1 Reply)
Discussion started by: jathin12
1 Replies

8. Shell Programming and Scripting

Grep multiple lines and save to a file

Sir I have a data file e.g. DATA31082009. This file consists of several data files appended to that file. The size of each data file is different. The first line of each file starts with "44". I want to grep data from "44" to the preceding line of next "44" and save it as a individual file.... (10 Replies)
Discussion started by: chssastry
10 Replies

9. Shell Programming and Scripting

Delete lines that starts with a certain letter

How can I delete those lines that starts with a certain letter? abc def ghi xyz abc def ace gik moq abe imq gxm I want to delete the line that starts with "x". Thanks! (4 Replies)
Discussion started by: kingpeejay
4 Replies
Login or Register to Ask a Question