例如,当我!
用作字段分隔符awk -F!
时,它会给出错误消息bash: !: event not found
。为什么?它正在接受awk -F"\!"
。重击版本 3.2.25
主页
/
user-343138
Kalpesh Bhoj's questions
Kalpesh Bhoj
Asked:
2020-02-25 08:06:54 +0800 CST
我有一个包含数据的大文本文件,格式如下:
1
2
3
4
5
6
7
8
9
10
我正在尝试将其转换为:
1 2 3
4 5 6
7 8 9
10
我试过awk
:
'{ if (NR%2) {printf "%40s\n", $0} else {printf "%80s\n", $0} }' file.txt
Kalpesh Bhoj
Asked:
2020-02-20 23:20:55 +0800 CST
我有一个名为 data.txt 的管道分隔文本文件,例如...
Kalpesh|100|1
Kalpesh|500|1
Ramesh|500|1
Ramesh|500|1
Ramesh|500|1
Naresh|500|1
Ganesh|500|1
Ganesh|500|1
Ganesh|500|1
Ganesh|500|1
我正在使用awk
如下脚本:
awk -F"|" 'BEGIN { ln=0;slno=0;pg=0; }
{
name=$1;
{
if (name !=x||ln > 50) #if same name repeates more than 50times then new page
{
tot=0;
pg++;
printf("\f");
print "PERSONS HAVING OUTSTANDING ADVANCE SALARY"
print "+==============================+"
print "|Sr.| name |Amount Rs.|Nos |"
print "+==============================+"
ln=0;
}
if (name!=x)
slno=1;tot+=$2;
{
printf ("|%3s|%10s|%10.2f|%4d|\n",slno,$1,$2,$3,tot,$4);
ln++;
slno++;
x=name;
}
}
} END {
print "================================"
print "Total for",$1,slno,tot
print "================================"
print "\f" }' data.txt
这给出了类似的结果
PERSONS HAVING OUTSTANDING ADVANCE SALARY
+==============================+
|Sr.| name |Amount Rs.|Nos |
+==============================+
| 1| Kalpesh| 100.00| 1|
| 2| Kalpesh| 500.00| 1|
PERSONS HAVING OUTSTANDING ADVANCE SALARY
+==============================+
|Sr.| name |Amount Rs.|Nos |
+==============================+
| 1| Ramesh| 500.00| 1|
| 2| Ramesh| 500.00| 1|
| 3| Ramesh| 500.00| 1|
PERSONS HAVING OUTSTANDING ADVANCE SALARY
+==============================+
|Sr.| name |Amount Rs.|Nos |
+==============================+
| 1| Naresh| 500.00| 1|
PERSONS HAVING OUTSTANDING ADVANCE SALARY
+==============================+
|Sr.| name |Amount Rs.|Nos |
+==============================+
| 1| Ganesh| 500.00| 1|
| 2| Ganesh| 500.00| 1|
| 3| Ganesh| 500.00| 1|
| 4| Ganesh| 500.00| 1|
================================
Total for Ganesh 5 2000
================================
我想要的输出就像
PERSONS HAVING OUTSTANDING ADVANCE SALARY
+==============================+
|Sr.| name |Amount Rs.|Nos |
+==============================+
| 1| Kalpesh| 100.00| 1|
| 2| Kalpesh| 500.00| 1|
================================
Total for Kalpesh 2 600
================================
PERSONS HAVING OUTSTANDING ADVANCE SALARY
+==============================+
|Sr.| name |Amount Rs.|Nos |
+==============================+
| 1| Ramesh| 500.00| 1|
| 2| Ramesh| 500.00| 1|
| 3| Ramesh| 500.00| 1|
================================
Total for Ramesh 3 1500
================================
PERSONS HAVING OUTSTANDING ADVANCE SALARY
+==============================+
|Sr.| name |Amount Rs.|Nos |
+==============================+
| 1| Naresh| 500.00| 1|
================================
Total for Naresh 1 500
================================
PERSONS HAVING OUTSTANDING ADVANCE SALARY
+==============================+
|Sr.| name |Amount Rs.|Nos |
+==============================+
| 1| Ganesh| 500.00| 1|
| 2| Ganesh| 500.00| 1|
| 3| Ganesh| 500.00| 1|
| 4| Ganesh| 500.00| 1|
================================
Total for Ganesh 5 2000
================================
Kalpesh Bhoj
Asked:
2020-02-10 23:43:21 +0800 CST
我有制表符分隔的文件如下:
Presence For Month 01/2020
Sl No Salary no Name 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr
1 111111 aaaaaaaaaaaaaaaaaaaaaaaa L A A A A A A A A A A A A A A A A A A A A A A A A
2 222222 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa P P P P P P P P P P A P P P P P P P P P P P F P P
我想计算他们的存在、缺席或从第 4 列开始的任何其他评论。这是按月计算的摘要,因此总天数可以是 28、29、30 或 31。
预期输出为:
Presence For Month 01/2020
Sl Salary no Name 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 TOTALS
We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr P A X ALL
1 111111 aaaaaaaaaaaaaaaaaaaaaaaa X A A A A A A A A A A A A A A A A A A A A A A A A P X X X X X 1 24 6 31
2 222222 aaaaaaaaaaaaaaaaaaaaaaaa P P P P P P P P P P A A A A A A A A A A X X X X X X X X X X P 11 10 10 31
headers=`awk -F"\t" 'BEGIN {
print "------------------------------------------------------------------------------------------------------------------------------------------------------------------------------"}{
if (NR==2) { printf "%5s|%9s|%-35s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%16s\n","Sl","SR NO","NAME",$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$30,$31,$32,$33,$34,"TOTALS"}
if (NR==3) { printf "%5s|%9s|%-35s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%16s\n","Sl","SR NO","NAME",$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$30,$31,$32,$33,$34,"TOTALS"} } END {
print "------------------------------------------------------------------------------------------------------------------------------------------------------------------------------"} ' con `
awk '$1~/[0-9]/{ print $0}' con | tr '\t' '|' | tr -s ' ' > details
awk -F"|" '{ print $2 }' details > sr_details
for sr in `cat sr_details`
do
echo "$sr" >.sr1
sr=`awk '{print $1}' .sr1`
#echo "`grep "^$sr" sr_details`" > sr2
grep "$sr" details > sr2
tot=`cat sr2 | tr '|' '\n' | awk 'NR>3 && $1~/[A-Z]/{ print $1 "|"}' | sort | uniq -c | tr '\n' '|'`
awk -F"|" -v tot="$tot" '{ print $0"|"tot }' sr2 >> final_details
done
awk -F"|" -v headers="$headers" 'BEGIN { printf "%s\n",headers } (NR%30)==0 { printf "%s\n",headers } {
printf "%5s|%9s|%-35s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s %2s %2s\n-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n",$1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$30,$31,$32,$33,$34,$35,$37,$39,$41 }' /root/Desktop/final_details > presence
rm -f final_details details sr_details sr2
Kalpesh Bhoj
Asked:
2020-02-07 22:28:17 +0800 CST
我想以如下格式输出日期...
01/01/2020
02/01/2020
03/01/2020
04/01/2020
05/01/2020
06/01/2020
07/01/2020
08/01/2020
09/01/2020
10/01/2020
11/01/2020
12/01/2020
13/01/2020
14/01/2020
15/01/2020
16/01/2020
17/01/2020
18/01/2020
19/01/2020
20/01/2020
21/01/2020
22/01/2020
23/01/2020
24/01/2020
25/01/2020
26/01/2020
27/01/2020
28/01/2020
29/01/2020
30/01/2020
目前我正在使用...
cal 01 2020 | grep -v "[A-Z][a-z]"| tr ' ' '\n' | sort -n | awk '$1~/[0-9]/ { printf "%02s/%02s/%4s\n",$1,"01","2020"}'
我希望上述格式用于01/01/2017
。31/12/2019
我该怎么做
Kalpesh Bhoj
Asked:
2019-09-03 10:11:54 +0800 CST
我最近在这里问了同样的问题,但我有一些不同的问题。前任:
输入文件
******************
.WER
+ aaa bbb ccc
+ ddd eee
******************
.SDF
+ zzz xxx yyy
+ iii
+ kkk lll
******************
.XCV
+ uuu vvv ggg
+ hhh qqq
******************
期望的输出:
******************
.WER aaa bbb ccc ddd eee
******************
.SDF zzz xxx yyy iii kkk lll
******************
.XCV uuu vvv ggg hhh qqq
******************
所以按照上面我有文件,其中没有行是不确定的。可以是 2,3 或 4,每条记录用 ****************** 分隔是否可以将每条记录的所有行转换为单行?对于我已成功使用的确切行数paste -s -d ' \n'
。
Kalpesh Bhoj
Asked:
2019-08-31 03:44:24 +0800 CST
我有两个文本文件,例如
一个
1111|100|11/11/1111|a
2222|200|11/11/1111|a
3333|300|11/11/1111|a
4444|400|11/11/1111|a
b
1111|100|11/11/1111|a
1111|100|11/11/1111|a
2222|200|11/11/1111|a
3333|300|11/11/1111|a
5555|500|11/11/1111|a
我用过下面的命令
awk -F"|" '{ printf "%4s|%3s|\n",$1,$2 }' a | grep -v -f /dev/stdin b
哪个输出....
5555|500|11/11/1111|a
预期产出是
1111|100|11/11/1111|a
5555|500|11/11/1111|a
我怎样才能做到这一点grep
或awk
Kalpesh Bhoj
Asked:
2019-08-10 02:38:21 +0800 CST
我有像这个例子这样的文本文件:
a | 0 | 11
a | 0 | 22
a | 1 | 22
a | 1 | 22
我正在使用这个命令:
awk -F"|" '{ sum[$1"|"$2]+=$3 } END { for (user in sum) printf "%10d |%12.2f\n",user,sum[user] }' myfile
产生结果
0 | 33.00
0 | 44.00
我的预期结果
a | 0 | 33
a | 1 | 44