Numbersara lambobin layi zuwa daidaiton fitarwa na fayilolin rubutu a cikin tashar

game da kara layi

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:

layin cat

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 layi

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:

Layin nl -ba

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 dot layi

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 layi

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:

layin cat -n

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:

layin awk

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 farawa a 5

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 babu layin layi

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:

ku FNR

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:

layin sed

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 kankare layi

sed -n 7p archivo.txt

Yin amfani da ƙaramin umarni

para sanya lambar zuwa daidaitattun fitowar fayil ta amfani da ƙasa da, gudu:

kasa -n

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:

gaisuwa -n

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.


Bar tsokaci

Your email address ba za a buga. Bukata filayen suna alama da *

*

*

  1. Wanda ke da alhakin bayanan: Miguel Ángel Gatón
  2. Manufar bayanan: Sarrafa SPAM, sarrafa sharhi.
  3. Halacci: Yarda da yarda
  4. Sadarwar bayanan: Ba za a sanar da wasu bayanan ga wasu kamfanoni ba sai ta hanyar wajibcin doka.
  5. Ajiye bayanai: Bayanin yanar gizo wanda Occentus Networks (EU) suka dauki nauyi
  6. Hakkoki: A kowane lokaci zaka iyakance, dawo da share bayanan ka.

      ba wanda m

    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

      Miguel Quintana mai sanya hoto m

    Kyakkyawan taimako! Godiya mai yawa!

      rodrigo wata m

    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?