A cikin labarin na gaba zamu ga wasu hanyoyi zuwa numbersara lambobin layi zuwa fayilolin rubutu cewa muna gani daga m. Idan ka taba mamakin yadda zaka kara wadannan lambobin layi zuwa daidaiton fitowar fayilolin rubutu na Gnu / Linux, wannan gajeren labarin zaiyi kokarin bayanin hanyoyi daban-daban da zaka yi shi.
Akwai hanyoyi da yawa don ƙara lambobin layi zuwa fayil ɗin rubutu. Nan gaba zamu ga hanyoyi daban-daban guda 6 don yin shi. Ina tunanin cewa ban da waɗanda za a gani a ƙasa, za a sami wasu hanyoyi, amma don farawa, waɗannan sun isa.
Numbersara lambobin layi zuwa daidaitaccen fayil ɗin rubutu
Da farko, zamu kirkiri fayel da ake kira fayil.txt. A ciki zan ƙara linesan layuka don bayar da abun ciki, waɗanda za'a gano su. Don ganin ƙunshin fayil ɗin da na ƙirƙira, za mu buɗe tashar (Ctrl + Alt + T) kuma mu rubuta a ciki:
cat archivo.txt
Amfani da umarnin nl
Kamar yadda ake gani daga fitowar umarnin cat, fayil ɗin yana da layi 10, tare da uku cikinsu babu komai. Za mu ƙara lambobin layi ta amfani da nl umarni. Don yin haka, gudu:
nl archivo.txt
Kamar yadda aka gani a baya kama, umarnin nl yayi watsi da layukan wofi. Kawai ƙara lambobi zuwa layin da babu fanko. Idan kina so lamba duk layuka, gami da layuka marasa amfani, gudu:
nl -b a archivo.txt
Bugu da ƙari, don ƙarin tsabta lokacin karanta sakamakon, za mu iya ƙara alama bayan lambobi. Misali, don ƙara lokaci bayan lambobi, gudu:
nl -s "." archivo.txt
Kuna iya so daidaita nisa fitarwa. Don yin wannan, yi amfani da -w zaɓi tare da ƙimar canji kamar yadda aka nuna a cikin masu zuwa:
nl -w3 archivo.txt
Yin amfani da umarnin cat
Ana amfani da umarnin kyanwa don nuna abun cikin fayil, kamar yadda aka gani a farkon hoton wannan labarin. Idan kanaso ka kara lambobi zuwa fitowar fayil, amfani -n kamar yadda aka nuna a cikin masu zuwa:
cat -n archivo.txt
Kuna iya sha'awar wuce daidaitaccen fitarwa zuwa sabon fayil. Don yin haka, kawai dole ne ku gudanar da umarnin kamar yadda aka nuna a ƙasa:
cat -n archivo.txt > nuevoarchivo.txt
Amfani da awk command
Wannan wataƙila hanya ce mafi rikitarwa don sakamakon da aka samu. Don ƙarawa lambobin layi don fitar da fitarwa ta amfani da umarnin awk, gudu:
awk 'BEGIN{i=1} /.*/{printf "%d.% s\n",i,$0; i++}' archivo.txt
Kamar yadda kuka gani, na sanya lambar farawa azaman 1 a farkon siga. Zamu iya sanya kowane lambar farko da kuka zaba, misali 5, kamar yadda ake gani a ƙasa:
awk 'BEGIN{i=5} /.*/{printf "%d.% s\n",i,$0; i++}' archivo.txt
Yi amfani da umarni mai zuwa idan ba kwa son yin la'akari da layukan mara amfani:
awk 'BEGIN{i=0} {if($0 !~ /^$/) {printf ("%d.%s \n",i,$0); i++} else { print $0} } ' archivo.txt
Idan kuna tsammanin dokokin da ke sama suna da ɗan wahalar tunawa, yi amfani da umarni mai zuwa, wanda zai ba ku sakamako iri ɗaya kamar na farkon da aka nuna tare da umarnin AWK:
awk '{ print FNR " " $0 }' archivo.txt
Yin amfani da umarnin sed
Don ƙara lambar layi zuwa daidaitaccen fitowar fayil ta amfani da umarnin sed, gudu:
sed '/./=' archivo.txt | sed '/./N; s/\n/ /'
Umurnin sed yana da kyakkyawar alama don ambata. Zamu iya Nuna takamaiman layi daga fayil. Misali, don nuna layi na bakwai na fayil ɗinmu, gudu:
sed -n 7p archivo.txt
Yin amfani da ƙaramin umarni
para sanya lambar zuwa daidaitattun fitowar fayil ta amfani da ƙasa da, gudu:
less -N archivo.txt
Yin amfani da umarnin grep
Ana iya amfani da umarnin grep don bincika layin da ke ƙunshe da takamaiman rubutu. Idan kana so numbersara lambobin layi zuwa layukan da ke ƙunshe da rubutun bincike, gudu:
grep -n "línea" archivo.txt
Ka tuna cewa wannan umurnin zai ƙara lambobi ne kawai a layin da ke ɗauke da layin bincike. Hakanan, kamar yadda aka gani a cikin sikirin ɗin, yana launukan rubutun da aka bincika. Duk abin da ke cikin fayil ɗin da aka bayar za a tsallake.
Kuma shi ke nan, ko da ma dole in faɗi haka waɗannan umarni suna da wasu zaɓuɓɓuka waɗanda zasu iya haɓaka ko faɗaɗa wadanda aka bayyana a cikin wannan labarin. Don ƙarin bayani game da umarnin da aka ambata anan, zaku iya tuntuɓar mutum shafuka.
a cikin umarnin awk ba kwa buƙatar bincika maganganun yau da kullun "/.*/"
ta tsoho ya riga ya raba fayil ɗin zuwa layi kuma $ 0 ya ƙunshi layin, don haka
awk 'FARA {i = 1} {printf "% 03d:% s \ n", i, $ 0; i ++} 'file.txt
aiki guda (kuma mafi kyau) fiye da
awk 'FARA {i = 1} /.*/=printf "% d.% s \ n", i, $ 0; i ++} 'file.txt
Kyakkyawan taimako! Godiya mai yawa!
Kafin fara gaisuwa ta abokantaka.
Ina da matsala anan tare da rubutun mai zuwa.
Bayani kan layukan da suka dace da bincike ana samo su daga fayil kuma an adana su cikin canji.
Alal misali:
values = "awk -F«: »'' {bugu $ 1» »$ 2» »$ 3} 'hociporky | grep "201025" -n`
Sannan zaku iya amfani dasu ta hanyar canzawa sannan kuma ku nemi takamaiman layi a cikin fayil din.
Alal misali:
don darajar a cikin $ dabi'u; yi
sed -n "$ {darajar} p" fayil
aikata
Amma wannan yana aika kuskure
sed: -e magana # 1, char 1: umarnin da ba a sani ba: ``
Shin wani zai iya bayyana mani dalilin da ya sa za a warware wannan?