开发者

How can i grep a nearer word from a file?

开发者 https://www.devze.com 2022-12-18 12:23 出处:网络
How can i grep a nearer word from a file ? E.g 04-02-2010Workingday 05-02-2010Workingday 06-02-2010Workingday

How can i grep a nearer word from a file ?

E.g

04-02-2010  Workingday
05-02-2010  Workingday
06-02-2010  Workingday
07-02-2010  Holiday
08-02-2010  Workingday
09-02-2010  Workingday

I stored above data in a file 'feb2010',

By this commend i stored date in one variable date=date '+%d-%m-%Y'

if date is 06-02开发者_运维百科-2010 , i want to grep " 06-02-2010 Workingday "

and want to store the string Working day in a variable

  • How can i do this ?
  • Is there any other option ?


daytype=`grep $date feb2010 | cut -c13-`

The grep outputs the line, then the cut cuts off everything before the 13th character on that line. (Another possibility is cut -f3 -d' ', which outputs the field after the second space.) The result is stored in the variable daytype.

This assumes that the date occurs only once in the file.


#! /bin/bash

grep `date '+%d-%m-%Y'` feb2010 |
while read date type; do
  echo $type
done


using the bash shell

#!/bin/bash
mydate=$(date '+%d-%m-%Y')
while read -r d day
do
    case "$d" in
        "$mydate"*) echo $day;;
    esac
done < feb2010


type=($(grep $date feb2010))    # make an array
type=${type[1]}                 # only keep the second element
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号