Extract 2 characters in file

Hello,
I want to extract 2 characters of the first column of the one file, that file has 19 columns, in bash script.
the first column is some thing like this 2005-07-25, and
I want to extract 07. I wrote this code
awk '{ print $ substr ($1,6, 2)}, but out put is he whole
of the column: 2005-07-25!
can you help me?

Questions by Baran_22

Showing Answers 1 - 72 of 72 Answers

virathin

  • Oct 6th, 2010
 

1.) Say example ur file name is "new"

2005-07-25 | a | b | c

cut -d "|" -f 1 new > new2

now new2 has "2005-07-25"

cut -d "-" -f 2 new2 > new3

now new3 will have only "month" of each line.

gyana ranjan

  • Sep 7th, 2015
 

cut -d"-" -f2 <<< "2005-07-25"

  Was this answer useful?  Yes

Nagarjuna

  • Oct 9th, 2015
 

You can try this one will work fine!
for example your file like this...
2015-10-09 maxwell Australia
2015-10-16 root England
Here you want get only month of this first file..ok
Answer:::: cut -d " " -f 1 inputfilename | cut -d "-" -f2

  Was this answer useful?  Yes

Mayuresh Dhagamwar

  • Feb 1st, 2016
 

cut -c 1-2

  Was this answer useful?  Yes

Pravin Malode

  • Feb 4th, 2016
 

cut -c 6-7 inputfilename

  Was this answer useful?  Yes

SriSowmiya

  • Feb 12th, 2016
 

echo "2005-07-25" | cut -c6,7

  Was this answer useful?  Yes

Arjit

  • Feb 18th, 2016
 

echo "2007-07-25"|awk -F "-" {print$2}

  Was this answer useful?  Yes

abhilash kumar

  • Feb 25th, 2016
 

cat file1.txt | cut -d "-" f2

  Was this answer useful?  Yes

Manoj

  • Mar 3rd, 2016
 

cat filename | cut -d "-" -f2

  Was this answer useful?  Yes

Bunny

  • Apr 9th, 2016
 

To display any character from any line of the line:
sed -n p | cut -c , ;

  Was this answer useful?  Yes

Jithesh

  • Apr 20th, 2016
 

please try this
bash-4.3$ awk {print substr($1,6,2)} file
07

  Was this answer useful?  Yes

cpher

  • May 30th, 2016
 

cut -c 6,7 filename assuming file contains 2005-07-25

  Was this answer useful?  Yes

Manvendra

  • Jun 4th, 2016
 

awk -F - {print $2} filename

  Was this answer useful?  Yes

Vipin Kumar

  • Aug 18th, 2016
 

awk {print substr($1,1,2)} file.txt is the syntax for substring function

  Was this answer useful?  Yes

sandip kumar behera

  • Sep 4th, 2016
 

awk -F " " {print $2} filename

  Was this answer useful?  Yes

yugandhar

  • Sep 24th, 2016
 

cut -d " " -f 1 | awk -F "-" print $2

  Was this answer useful?  Yes

Rishu

  • Oct 21st, 2016
 

awk -F"-" {print $1} filename |cut -c1,2

  Was this answer useful?  Yes

Anupam Kumar

  • Oct 27th, 2016
 

awk -F"," {print $1} | awk -F"-" {prit $2}

  Was this answer useful?  Yes

Pankaj

  • Nov 17th, 2016
 

awk -F - {print substr($0,6,2)} 1.txt

  Was this answer useful?  Yes

Avinash Gupta

  • Mar 8th, 2017
 

cut -f1 test1 | cut -d - -f2

  Was this answer useful?  Yes

apoorva

  • Apr 25th, 2017
 

awk -F "-" {print substr($2,0,2)} xdf.txt

  Was this answer useful?  Yes

vishal singh

  • May 8th, 2017
 

Awk {print substr($1,1,2)} t1.txt
t1.txt
NAME AGE GENDER
SANTOSH 23 Male
ajay 24 Male
vijay 24 male

Code
  1.  

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions