cut the first word in the file

how to cut the first word in multiple file at a time

i.e)1st row and 1st colum word in multiple files

If your file is csv:

awk -F "," '{print $1}' *.csv

change "," with the other character in case its different.
Also your can provide the list of file instead of *.csv

---------- Post updated at 01:50 PM ---------- Previous update was at 01:45 PM ----------

Sorry for not reading the requirement properly.

for first row first field,

awk -F "," 'NR==1 {print $1}' *.csv 

its shows some error

 
 
awk: syntax error near line 1
awk: bailing out near line 1

let me explain clearly

i have a file called test
content of the file is

dsd|dfsd|Sdfsdf|sdff
sdfwert|5rygrd|trhyr|dsfsd
edfgsd|aserhtrh|hjfg|

my expected outpu is

dsd
# cat infile
dsd|dfsd|Sdfsdf|sdff
sdfwert|5rygrd|trhyr|dsfsd
edfgsd|aserhtrh|hjfg|
# sed -n '1s/^\([a-z]*\)|.*/\1/p' infile
dsd
1 Like
awk -F "|" 'NR==1 {print $1}' test

Use nawk or /usr/xpg4/bin/awk on solaris.

1 Like

thanks team , its working . . .

my next aim is

i have a file called infile

 
$ cat infile
test.txt
test1.txt
test2.txt

test.txt contain

 
asd|asdas|asdas
asd1|asdasrf|sadfsa
sdfwef|rfgdf|dfe

test1.txt contain

 
 
wsd|erfe|rtger|ewrf
rgfd|thgfd|gdf|rfgv
dsfsdf|rygdf|hgbdf

test2.txt conatin

 
 
qsd|fg|fdgdr
fdgv|fdg|fdtr
trhb|dfs|rfg

my excepted output will be

 
 
asd
wsd
qsd

Hi,

#!/bin/sh

while read filename
do
awk -F "|" 'NR==1 {print $1}' $filename
done < infile