ฉันจะแทนที่ช่องว่างด้วยแท็บใน linux ในไฟล์ข้อความที่กำหนดได้อย่างไร
ฉันจะแทนที่ช่องว่างด้วยแท็บใน linux ในไฟล์ข้อความที่กำหนดได้อย่างไร
คำตอบ:
UNEXPAND(1)                      User Commands                     UNEXPAND(1)
NAME
       unexpand - convert spaces to tabs
SYNOPSIS
       unexpand [OPTION]... [FILE]...
DESCRIPTION
       Convert  blanks in each FILE to tabs, writing to standard output.  With
       no FILE, or when FILE is -, read standard input.
       Mandatory arguments to long options are  mandatory  for  short  options
       too.
       -a, --all
              convert all blanks, instead of just initial blanks
       --first-only
              convert only leading sequences of blanks (overrides -a)
       -t, --tabs=N
              have tabs N characters apart instead of 8 (enables -a)
       -t, --tabs=LIST
              use comma separated LIST of tab positions (enables -a)
       --help display this help and exit
       --version
              output version information and exit
. . .
STANDARDS
       The expand and unexpand utilities conform to IEEE Std 1003.1-2001
       (``POSIX.1'').
    ฉันคิดว่าคุณสามารถลองด้วย awk
awk -v OFS="\t" '$1=$1' file1
หรือ SED หากคุณต้องการ
sed 's/[:blank:]+/,/g' thefile.txt > the_modified_copy.txt
หรือแม้กระทั่ง tr
tr -s '\t' < thefile.txt | tr '\t' ' ' > the_modified_copy.txt
หรือเวอร์ชันที่เรียบง่ายของโซลูชัน tr ที่แซมบิสบีเสนอ
tr ' ' \\t < someFile > someFile
    tr ' ' \\t < someFile > someFile
                    ls -l | sed "s/ \+/ /g"
                    awk -v OFS="\t" '$1=$1' file1ฉันสังเกตเห็นว่าถ้าคุณมีบรรทัดที่ขึ้นต้นด้วยหมายเลข 0 (เช่น0   1   2) บรรทัดจะถูกกำหนดจากผลลัพธ์
                    การใช้ Perl :
perl -p -i -e 's/ /\t/g' file.txt
    perl -p -i -e 's/\t/  /g' *.java
                    s/ {4}/แปลงการเยื้อง 4 ช่องว่างเป็นแท็บ
                    คำสั่งtr ที่ดีกว่า:
tr [:blank:] \\t
สิ่งนี้จะล้างผลลัพธ์ของ say, unzip -l , สำหรับการประมวลผลเพิ่มเติมด้วย grep, cut ฯลฯ
เช่น,
unzip -l some-jars-and-textfiles.zip | tr [:blank:] \\t | cut -f 5 | grep jar
    tr [:blank:] \\t
                    ดาวน์โหลดและเรียกใช้สคริปต์ต่อไปนี้เพื่อแปลงซอฟต์แท็บซ้ำเป็นฮาร์ดแท็บในไฟล์ข้อความธรรมดา
วางและเรียกใช้สคริปต์จากภายในโฟลเดอร์ซึ่งมีไฟล์ข้อความธรรมดา
#!/bin/bash
find . -type f -and -not -path './.git/*' -exec grep -Iq . {} \; -and -print | while read -r file; do {
    echo "Converting... "$file"";
    data=$(unexpand --first-only -t 4 "$file");
    rm "$file";
    echo "$data" > "$file";
}; done;
    ตัวอย่างคำสั่งสำหรับการแปลงไฟล์. js แต่ละไฟล์ภายใต้ dir ปัจจุบันเป็นแท็บ (แปลงเฉพาะช่องว่างนำหน้า):
find . -name "*.js" -exec bash -c 'unexpand -t 4 --first-only "$0" > /tmp/totabbuff && mv /tmp/totabbuff "$0"' {} \;
    astyleนอกจากนี้คุณยังสามารถใช้ ฉันพบว่ามันมีประโยชน์มากและมีหลายตัวเลือกด้วย:
Tab and Bracket Options:
   If  no  indentation  option is set, the default option of 4 spaces will be used. Equivalent to -s4 --indent=spaces=4.  If no brackets option is set, the
   brackets will not be changed.
   --indent=spaces, --indent=spaces=#, -s, -s#
          Indent using # spaces per indent. Between 1 to 20.  Not specifying # will result in a default of 4 spaces per indent.
   --indent=tab, --indent=tab=#, -t, -t#
          Indent using tab characters, assuming that each tab is # spaces long.  Between 1 and 20. Not specifying # will result in a default assumption  of
          4 spaces per tab.`
    tr -s '[:blank:]' '\t'หากจะพูดถึงเกี่ยวกับการเปลี่ยนพื้นที่ติดต่อกันทั้งหมดบนเส้นด้วยแท็บแล้ว
[root@sysresccd /run/archiso/img_dev]# sfdisk -l -q -o Device,Start /dev/sda
Device         Start
/dev/sda1       2048
/dev/sda2     411648
/dev/sda3    2508800
/dev/sda4   10639360
/dev/sda5   75307008
/dev/sda6   96278528
/dev/sda7  115809778
[root@sysresccd /run/archiso/img_dev]# sfdisk -l -q -o Device,Start /dev/sda | tr -s '[:blank:]' '\t'
Device  Start
/dev/sda1       2048
/dev/sda2       411648
/dev/sda3       2508800
/dev/sda4       10639360
/dev/sda5       75307008
/dev/sda6       96278528
/dev/sda7       115809778
หากจะพูดถึงเกี่ยวกับการเปลี่ยนช่องว่างทั้งหมด (พื้นที่เช่นแท็บบรรทัดใหม่ ฯลฯ ) tr -s '[:space:]'แล้ว
[root@sysresccd /run/archiso/img_dev]# sfdisk -l -q -o Device,Start /dev/sda | tr -s '[:space:]' '\t'
Device  Start   /dev/sda1       2048    /dev/sda2       411648  /dev/sda3       2508800 /dev/sda4       10639360        /dev/sda5       75307008        /dev/sda6     96278528        /dev/sda7       115809778  
หากคุณกำลังพูดถึงการแก้ไขไฟล์ที่เสียหายจากแท็บให้ใช้expandและunexpandตามที่กล่าวไว้ในคำตอบอื่น ๆ
ใช้sed :
T=$(printf "\t")
sed "s/[[:blank:]]\+/$T/g"
หรือ
sed "s/[[:space:]]\+/$T/g"
    สิ่งนี้จะแทนที่ช่องว่างที่ติดต่อกันด้วยช่องว่างเดียว (แต่ไม่ใช่แท็บ)
tr -s '[:blank:]'
สิ่งนี้จะแทนที่ช่องว่างที่ติดต่อกันด้วยแท็บ
tr -s '[:blank:]' '\t'
    -cมันจะแทนที่อักขระที่ต่อเนื่องกันที่ไม่ใช่ช่องว่าง
                    
trsed