亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

Chinaunix

標(biāo)題: 請教怎樣從讀入文件中抽出第一列的值,然后輸出 [打印本頁]

作者: robinhappiness    時(shí)間: 2016-02-21 20:06
標(biāo)題: 請教怎樣從讀入文件中抽出第一列的值,然后輸出
如題,請教各位前輩,用shell命令應(yīng)該如何實(shí)現(xiàn)?多謝了!

想要實(shí)現(xiàn)的功能如下:

讀入文件
$cat inuput.txt
#comment
id,name,country,city
abc_0001,john,,
abc_0002,lee,,
abc_0003,jack,,
abc_0004,adam,,

命令執(zhí)行之后,將讀入文件inuput.txt中的id列的值的list取出,然后輸出如下文件(list內(nèi)容之間由空格隔開)。
$cat output.txt
abc_0001 abc_0002 abc_0003 abc_0004
作者: zxy877298415    時(shí)間: 2016-02-21 20:53
  1. awk -F, 'NR>2{t=t?t" "$1:$1}END{print t>"output.txt"}' file
復(fù)制代碼
回復(fù) 1# robinhappiness


   
作者: moperyblue    時(shí)間: 2016-02-22 00:45
本帖最后由 moperyblue 于 2016-02-22 00:54 編輯
  1. sed -ne '1,2d;s/,.*//;3{h;b};H;${g;s/\n/ /g;w file.txt' -e'}' file
復(fù)制代碼

作者: jason680    時(shí)間: 2016-02-22 07:58
$ awk -F, 'id{p=p c $1;c=" "}/^id/{id=1}END{print p}' FILE
abc_0001 abc_0002 abc_0003 abc_0004
作者: robinhappiness    時(shí)間: 2016-02-22 08:12
謝謝各位前輩!學(xué)習(xí)了!
作者: robinhappiness    時(shí)間: 2016-02-22 08:15
回復(fù) 4# jason680

謝謝!
我在輸入如下命令之后,然后在命令行下顯示 >  
請問這是什么原因?

$ awk -F, 'id{p=p c $1;c=" "}/^id/{id=1}END{print p}' FILE
>
作者: jcdiy0601    時(shí)間: 2016-02-22 09:22
  1. awk -F, 'NR>2{printf("%s ",$1) > "output.txt"}' input.txt
復(fù)制代碼

作者: robinhappiness    時(shí)間: 2016-02-22 12:40
本帖最后由 robinhappiness 于 2016-02-22 12:41 編輯

謝謝各位!不好意思 追加一個(gè)問題,如果id列的值前面存在#的話,#行的id將不被輸出到output.txt文件中,請教怎樣實(shí)現(xiàn)?

比如,讀入文件
$cat inuput.txt
#comment
id,name,country,city
#abc_0001,john,,
abc_0002,lee,,
#abc_0003,jack,,
abc_0004,adam,,

命令執(zhí)行之后,將讀入文件inuput.txt中的id列的值(前面帶有#號的id除外)的list取出,然后輸出如下文件(list內(nèi)容之間由空格隔開)。
$cat output.txt
abc_0002 abc_0004
作者: baby_神    時(shí)間: 2016-02-22 13:18
本帖最后由 baby_神 于 2016-02-22 13:18 編輯

grep -v '^#' inuput.txt | awk -F, 'NR>2{t=t?t" "$1:$1}END{print t>"output.txt"}'
回復(fù) 8# robinhappiness


   
作者: moperyblue    時(shí)間: 2016-02-22 13:53
  1. sed -ne '1,2d;/^#/b;s/,.*//;H;${g;s/\n//;s/\n/ /gp;w file.txt' -e'}' file
復(fù)制代碼

作者: wh7211    時(shí)間: 2016-02-22 14:57
回復(fù) 8# robinhappiness
  1. awk -F"," 'NR>2&&/^[^#]/{a=a?a" "$1:$1}END{print a>"output.txt"}' file
復(fù)制代碼

作者: lmqytz    時(shí)間: 2016-02-23 10:32
grep -o 'abc_000.' 1 | awk '{printf $1" "}'




歡迎光臨 Chinaunix (http://www.72891.cn/) Powered by Discuz! X3.2