如何重新定位每行的第三个字符串以从同一行的第 n 个字符开始
How to reposition the third string of each line to start at the nth character on the same line
Ubuntu 16.04
Bash 4.4.20
我想清理我的一些游戏配置,以便信息整齐地存储在列中。每行都有类似的内容:
somegamevar "0" // This here explains the stuff on the left
anothervar 9 // Sometimes the value is not in doublequotes
如果文件的分栏不整齐,它可能会变得凌乱,因此我们会在编辑器中花费大量时间。这是其中一个文件的一部分。
mp_death_drop_defuser "1" // Drop defuser on player death
mp_death_drop_grenade "2" // Which grenade to drop on player death: 0=none, 1=best, 2=current or best
mp_death_drop_gun "1" // Which gun to drop on player death: 0=none, 1=best, 2=current or best
mp_defuser_allocation "0" // How to allocate defusers to CTs at start or round: 0=none, 1=random, 2=everyone
mp_do_warmup_period "1" // Whether or not to do a warmup period at the start of a match.
mp_forcecamera "1" // Restricts spectator modes for dead players
mp_force_pick_time "160" // The amount of time a player has on the team screen to make a selection before being auto-teamed
mp_free_armor "0" // Determines whether armor and helmet are given automatically.
mp_freezetime "12" // How many seconds to keep players frozen when the round starts
mp_friendlyfire "1" // Allows team members to injure other members of their team
mp_halftime "1" // Determines whether or not the match has a team-swapping halftime event.
mp_halftime_duration "15" // Number of seconds that halftime lasts
mp_join_grace_time "30" // Number of seconds after round start to allow a player to join a game
mp_limitteams "0" // Max # of players 1 team can have over another (0 disables check)
mp_logdetail "3" // Logs attacks. Values are: 0=off, 1=enemy, 2=teammate, 3=both)
mp_match_can_clinch "1" // Can a team clinch and end the match by being so far ahead that the other team has no way to catching up
mp_match_end_restart "1" // At the end of the match, perform a restart instead of loading a new map
mp_maxmoney "16000" // maximum amount of money allowed in a player's account
mp_maxrounds "30" // max number of rounds to play before server changes maps
mp_molotovusedelay "0" // Number of seconds to delay before the molotov can be used after acquiring it
mp_overtime_enable "1" // Use overtime rules to determine winner
mp_overtime_maxrounds "10"
mp_overtime_startmoney "16000"
mp_playercashawards "1" // Players can earn money by performing in-game actions
mp_playerid "0" // Controls what information player see in the status bar: 0 all names; 1 team names; 2 no names
mp_playerid_delay "0.5" // Number of seconds to delay showing information in the status bar
mp_playerid_hold "0.25" // Number of seconds to keep showing old information in the status bar
mp_round_restart_delay "5" // Number of seconds to delay before restarting a round after a win
mp_roundtime "1.75" // How many minutes each round takes.
mp_roundtime_defuse "1.75" // How many minutes each round takes on defusal maps.
mp_solid_teammates "1" // Determines whether teammates are solid or not.
mp_startmoney "800" // amount of money each player gets when they reset
mp_teamcashawards "1" // Teams can earn money by performing in-game actions
mp_timelimit "0" // game time per map in minutes
mp_tkpunish "0" // Will a TK'er be punished in the next round? {0=no, 1=yes}
mp_warmuptime "1" // If true, there will be a warmup period/round at the start of each match to allow
mp_weapons_allow_map_placed "1" // If this convar is set, when a match starts, the game will not delete weapons placed in the map.
mp_weapons_allow_zeus "1" // Determines whether the Zeus is purchasable or not.
mp_win_panel_display_time "15" // The amount of time to show the win panel between matches / halfs
spec_freeze_time "5.0" // Time spend frozen in observer freeze cam.
spec_freeze_panel_extended_time "0" // Time spent with the freeze panel still up after observer freeze cam is done.
sv_accelerate "5.6" // ( def. "10" ) client notify replicated
sv_allow_votes "0" // Allow voting?
sv_allow_wait_command "0" // Allow or disallow the wait command on clients connected to this server.
sv_alltalk "0" // Players can hear all other players' voice communication, no team restrictions
sv_alternateticks "0" // If set, server only simulates entities on even numbered ticks.
sv_cheats "0" // Allow cheats on server
sv_clockcorrection_msecs "15" // The server tries to keep each player's m_nTickBase withing this many msecs of the server absolute tickcount
sv_consistency "0" // Whether the server enforces file consistency for critical files
sv_contact "0" // Contact email for server sysop
sv_damage_print_enable "1" // Turn this off to disable the player's damage feed in the console after getting killed.
sv_dc_friends_reqd "0" // Set this to 0 to allow direct connects to a game in progress even if no presents
sv_deadtalk "1" // Dead players can speak (voice, text) to the living
sv_forcepreload "0" // Force server side preloading.
sv_friction "4.8" // World friction.
sv_full_alltalk "0" // Any player (including Spectator team) can speak to any other player
sv_gameinstructor_disable "1" // Force all clients to disable their game instructors.
sv_ignoregrenaderadio "0" // Turn off Fire in the hole messages
sv_kick_players_with_cooldown 0 // (0: do not kick; 1: kick Untrusted players; 2: kick players with any cooldown)
sv_kick_ban_duration "0" // How long should a kick ban from the server should last (in minutes)
sv_lan "0" // Server is a lan server ( no heartbeat, no authentication, no non-class C addresses )
sv_log_onefile "0" // Log server information to only one file.
sv_logbans "1" // Log server bans in the server logs.
sv_logecho "1" // Echo log information to the console.
sv_logfile "1" // Log server information in the log file.
sv_logflush "0" // Flush the log file to disk on each write (slow).
sv_logsdir "logfiles" // Folder in the game directory where server logs will be stored.
sv_maxrate "0" // min. 0.000000 max. 30000.000000 replicated Max bandwidth rate allowed on server, 0 == unlimited
如果没有第三列,column -t
会很棒,但第三列很重要。
所以这是预期的结果:
第 1 列和第 2 列相互隔开几个空格。
第 3 列从第 70 个字符开始。
sv_alltalk "0" // Players can hear all other players voice communication, no team restrictions
sv_alternateticks "0" // If set, server only simulates entities on even numbered ticks.
sv_cheats "0" // Allow cheats on server
sv_clockcorrection_msecs "15" // The server tries to keep each players m_nTickBase withing this many msecs of the server absolute tickcount
sv_consistency "0" // Whether the server enforces file consistency for critical files
sv_contact "0" // Contact email for server sysop
sv_damage_print_enable "1" // Turn this off to disable the players damage feed in the console after getting killed.
sv_dc_friends_reqd "0" // Set this to 0 to allow direct connects to a game in progress even if no presents
sv_deadtalk "1" // Dead players can speak (voice, text) to the living
sv_forcepreload "0" // Force server side preloading.
sv_friction "4.8" // World friction.
sv_full_alltalk "0" // Any player (including Spectator team) can speak to any other player
sv_gameinstructor_disable "1" // Force all clients to disable their game instructors.
sv_ignoregrenaderadio "0" // Turn off Fire in the hole messages
sv_kick_players_with_cooldown 0 // (0: do not kick; 1: kick Untrusted players; 2: kick players with any cooldown)
sv_kick_ban_duration "0" // How long should a kick ban from the server should last (in minutes)
sv_lan "0" // Server is a lan server ( no heartbeat, no authentication, no non-class C addresses )
sv_log_onefile "0" // Log server information to only one file.
sv_logbans "1" // Log server bans in the server logs.
sv_logecho "1" // Echo log information to the console.
sv_logfile "1" // Log server information in the log file.
sv_logflush "0" // Flush the log file to disk on each write (slow).
sv_logsdir "logfiles" // Folder in the game directory where server logs will be stored.
sv_maxrate "0" // min. 0.000000 max. 30000.000000 replicated Max bandwidth rate allowed on server, 0 == unlimited
正如您在上面看到的,第 1 列和第 2 列彼此靠近,第 3 列从第 70 列开始整齐地向右。
您能否尝试使用显示的示例进行以下、编写和测试。
awk '
FNR==NR{
first_len=(first_len>length()?first_len:length())
second_len=(second_len>length()?second_len:length())
next
}
{
val=""
match([=10=],/\/\/.*/)
val=substr([=10=],RSTART,RLENGTH)
[=10=]=substr([=10=],1,RSTART-1)
printf("%-"first_len"s\t\t%-"second_len"s\t\t%s\n",,,val)
}
' Input_file Input_file
解释:为以上添加详细解释。
awk ' ##Starting awk program from here.
FNR==NR{ ##Checking condition when Input_file is being read first time.
first_len=(first_len>length()?first_len:length()) ##Creating first_len which has length of 1st field.
second_len=(second_len>length()?second_len:length()) ##Creating second_len which has length of 2nd field.
next ##next will skip all further statements from here.
}
{
val="" ##Nullifying next statements from here.
match([=11=],/\/\/.*/) ##Using match function to match from // to till end of line.
val=substr([=11=],RSTART,RLENGTH) ##Creating val which has sub string of matched regex.
[=11=]=substr([=11=],1,RSTART-1) ##Keeping everything before matched regex in current line.
printf("%-"first_len"s\t\t%-"second_len"s\t\t%s\n",,,val) ##Printing spaces appropriately after fields to even the spaces.
}
' Input_file Input_file ##Mentioning Input_file name here.
注意: 我在输出中放置了 2 个选项卡,以防您需要单个选项卡(每列的对齐方式已经固定)然后在上面的 printf 语句中从 \t\t
更改为 \t
。
使用 GNU awk
和 column
,前提是您知道输入中不能出现的某些字符,例如 4
。此外,它假定前两列不能有 space 字符,正如从问题中显示的示例输入中推断的那样。
awk -v FPAT='//.*$|[^ ]+' -v OFS='4' '{=} 1' ip.txt | column -s$'4' -t
- 此处
FPAT
用于定义字段内容的模式。 //.*$
将获取从 //
到行尾的所有内容。 [^ ]+
定义前两个字段的字段内容。
-v OFS='4'
将输入中不存在的一些字符设置为输出字段分隔符
{=} 1
重新构建 [=20=]
的内容并打印
column -s$'4' -t
将根据 4
字段分隔符 整齐地对齐输出
$ cat tst.awk
NR==FNR {
wid = (length() > wid ? length() : wid)
next
}
NF {
hd = sprintf("%-*s%3s%s", wid, , "", )
tl = ( (s=index([=10=],"//")) ? substr([=10=],s) : "")
[=10=] = sprintf("%-69s%s", hd, tl)
}
{ print }
$ awk -f tst.awk file file
mp_death_drop_defuser "1" // Drop defuser on player death
mp_death_drop_gun "1" // Which gun to drop on player death: 0=none, 1=best, 2=current or best
mp_friendlyfire "1" // Allows team members to injure other members of their team
mp_halftime "1" // Determines whether or not the match has a team-swapping halftime event.
mp_overtime_startmoney "16000"
mp_playercashawards "1" // Players can earn money by performing in-game actions
sv_alltalk "0" // Players can hear all other players' voice communication, no team restrictions
sv_cheats "0" // Allow cheats on server
sv_forcepreload "0" // Force server side preloading.
原答案:
这将按照您的要求从第 3 列的字符位置 60 开始:
awk '
s = index([=12=],"//") {
hd = substr([=12=],1,s-1)
sub(/[[:space:]]+$/,"",hd)
[=12=] = sprintf("%-59s%s",hd,substr([=12=],s))
}
1' file
例如鉴于此输入:
$ cat file
mp_death_drop_defuser "1" // Drop defuser on player death
mp_death_drop_gun "1" // Which gun to drop on player death: 0=none, 1=best, 2=current or best
mp_friendlyfire "1" // Allows team members to injure other members of their team
mp_halftime "1" // Determines whether or not the match has a team-swapping halftime event.
mp_overtime_startmoney "16000"
mp_playercashawards "1" // Players can earn money by performing in-game actions
sv_alltalk "0" // Players can hear all other players' voice communication, no team restrictions
sv_cheats "0" // Allow cheats on server
sv_forcepreload "0" // Force server side preloading.
$ awk 's=index([=14=],"//"){hd=substr([=14=],1,s-1); sub(/[[:space:]]+$/,"",hd); [=14=]=sprintf("%-59s%s",hd,substr([=14=],s))} 1' file
mp_death_drop_defuser "1" // Drop defuser on player death
mp_death_drop_gun "1" // Which gun to drop on player death: 0=none, 1=best, 2=current or best
mp_friendlyfire "1" // Allows team members to injure other members of their team
mp_halftime "1" // Determines whether or not the match has a team-swapping halftime event.
mp_overtime_startmoney "16000"
mp_playercashawards "1" // Players can earn money by performing in-game actions
sv_alltalk "0" // Players can hear all other players' voice communication, no team restrictions
sv_cheats "0" // Allow cheats on server
sv_forcepreload "0" // Force server side preloading.
为了获得漂亮的输出,我会首先尝试使用分隔符重新格式化您的输入以满足 column -t
。例如,您可以使用 sed 仅对前两列进行模式匹配,并在适当的地方插入一对 tab-characters:
sed 's%^\([^ ]*\) *\([^ ]*\) *%\t\t%' config.txt | column -Lts$'\t'
不过,要获得更强大的解决方案,请考虑使用 python and the str.split() 函数。这可以限制返回的元素数量(使用 'maxsplit' arg)使得再次加入 tab-character 变得非常简单。将以下内容放入名为 format_config.py
的文件中:
import sys
for line in sys.stdin:
sys.stdout.write('\t'.join(line.strip().split(None, 2)) + '\n')
然后:
python format_config.py < config.txt | column -Lts$'\t'
Ubuntu 16.04
Bash 4.4.20
我想清理我的一些游戏配置,以便信息整齐地存储在列中。每行都有类似的内容:
somegamevar "0" // This here explains the stuff on the left
anothervar 9 // Sometimes the value is not in doublequotes
如果文件的分栏不整齐,它可能会变得凌乱,因此我们会在编辑器中花费大量时间。这是其中一个文件的一部分。
mp_death_drop_defuser "1" // Drop defuser on player death
mp_death_drop_grenade "2" // Which grenade to drop on player death: 0=none, 1=best, 2=current or best
mp_death_drop_gun "1" // Which gun to drop on player death: 0=none, 1=best, 2=current or best
mp_defuser_allocation "0" // How to allocate defusers to CTs at start or round: 0=none, 1=random, 2=everyone
mp_do_warmup_period "1" // Whether or not to do a warmup period at the start of a match.
mp_forcecamera "1" // Restricts spectator modes for dead players
mp_force_pick_time "160" // The amount of time a player has on the team screen to make a selection before being auto-teamed
mp_free_armor "0" // Determines whether armor and helmet are given automatically.
mp_freezetime "12" // How many seconds to keep players frozen when the round starts
mp_friendlyfire "1" // Allows team members to injure other members of their team
mp_halftime "1" // Determines whether or not the match has a team-swapping halftime event.
mp_halftime_duration "15" // Number of seconds that halftime lasts
mp_join_grace_time "30" // Number of seconds after round start to allow a player to join a game
mp_limitteams "0" // Max # of players 1 team can have over another (0 disables check)
mp_logdetail "3" // Logs attacks. Values are: 0=off, 1=enemy, 2=teammate, 3=both)
mp_match_can_clinch "1" // Can a team clinch and end the match by being so far ahead that the other team has no way to catching up
mp_match_end_restart "1" // At the end of the match, perform a restart instead of loading a new map
mp_maxmoney "16000" // maximum amount of money allowed in a player's account
mp_maxrounds "30" // max number of rounds to play before server changes maps
mp_molotovusedelay "0" // Number of seconds to delay before the molotov can be used after acquiring it
mp_overtime_enable "1" // Use overtime rules to determine winner
mp_overtime_maxrounds "10"
mp_overtime_startmoney "16000"
mp_playercashawards "1" // Players can earn money by performing in-game actions
mp_playerid "0" // Controls what information player see in the status bar: 0 all names; 1 team names; 2 no names
mp_playerid_delay "0.5" // Number of seconds to delay showing information in the status bar
mp_playerid_hold "0.25" // Number of seconds to keep showing old information in the status bar
mp_round_restart_delay "5" // Number of seconds to delay before restarting a round after a win
mp_roundtime "1.75" // How many minutes each round takes.
mp_roundtime_defuse "1.75" // How many minutes each round takes on defusal maps.
mp_solid_teammates "1" // Determines whether teammates are solid or not.
mp_startmoney "800" // amount of money each player gets when they reset
mp_teamcashawards "1" // Teams can earn money by performing in-game actions
mp_timelimit "0" // game time per map in minutes
mp_tkpunish "0" // Will a TK'er be punished in the next round? {0=no, 1=yes}
mp_warmuptime "1" // If true, there will be a warmup period/round at the start of each match to allow
mp_weapons_allow_map_placed "1" // If this convar is set, when a match starts, the game will not delete weapons placed in the map.
mp_weapons_allow_zeus "1" // Determines whether the Zeus is purchasable or not.
mp_win_panel_display_time "15" // The amount of time to show the win panel between matches / halfs
spec_freeze_time "5.0" // Time spend frozen in observer freeze cam.
spec_freeze_panel_extended_time "0" // Time spent with the freeze panel still up after observer freeze cam is done.
sv_accelerate "5.6" // ( def. "10" ) client notify replicated
sv_allow_votes "0" // Allow voting?
sv_allow_wait_command "0" // Allow or disallow the wait command on clients connected to this server.
sv_alltalk "0" // Players can hear all other players' voice communication, no team restrictions
sv_alternateticks "0" // If set, server only simulates entities on even numbered ticks.
sv_cheats "0" // Allow cheats on server
sv_clockcorrection_msecs "15" // The server tries to keep each player's m_nTickBase withing this many msecs of the server absolute tickcount
sv_consistency "0" // Whether the server enforces file consistency for critical files
sv_contact "0" // Contact email for server sysop
sv_damage_print_enable "1" // Turn this off to disable the player's damage feed in the console after getting killed.
sv_dc_friends_reqd "0" // Set this to 0 to allow direct connects to a game in progress even if no presents
sv_deadtalk "1" // Dead players can speak (voice, text) to the living
sv_forcepreload "0" // Force server side preloading.
sv_friction "4.8" // World friction.
sv_full_alltalk "0" // Any player (including Spectator team) can speak to any other player
sv_gameinstructor_disable "1" // Force all clients to disable their game instructors.
sv_ignoregrenaderadio "0" // Turn off Fire in the hole messages
sv_kick_players_with_cooldown 0 // (0: do not kick; 1: kick Untrusted players; 2: kick players with any cooldown)
sv_kick_ban_duration "0" // How long should a kick ban from the server should last (in minutes)
sv_lan "0" // Server is a lan server ( no heartbeat, no authentication, no non-class C addresses )
sv_log_onefile "0" // Log server information to only one file.
sv_logbans "1" // Log server bans in the server logs.
sv_logecho "1" // Echo log information to the console.
sv_logfile "1" // Log server information in the log file.
sv_logflush "0" // Flush the log file to disk on each write (slow).
sv_logsdir "logfiles" // Folder in the game directory where server logs will be stored.
sv_maxrate "0" // min. 0.000000 max. 30000.000000 replicated Max bandwidth rate allowed on server, 0 == unlimited
如果没有第三列,column -t
会很棒,但第三列很重要。
所以这是预期的结果:
第 1 列和第 2 列相互隔开几个空格。
第 3 列从第 70 个字符开始。
sv_alltalk "0" // Players can hear all other players voice communication, no team restrictions
sv_alternateticks "0" // If set, server only simulates entities on even numbered ticks.
sv_cheats "0" // Allow cheats on server
sv_clockcorrection_msecs "15" // The server tries to keep each players m_nTickBase withing this many msecs of the server absolute tickcount
sv_consistency "0" // Whether the server enforces file consistency for critical files
sv_contact "0" // Contact email for server sysop
sv_damage_print_enable "1" // Turn this off to disable the players damage feed in the console after getting killed.
sv_dc_friends_reqd "0" // Set this to 0 to allow direct connects to a game in progress even if no presents
sv_deadtalk "1" // Dead players can speak (voice, text) to the living
sv_forcepreload "0" // Force server side preloading.
sv_friction "4.8" // World friction.
sv_full_alltalk "0" // Any player (including Spectator team) can speak to any other player
sv_gameinstructor_disable "1" // Force all clients to disable their game instructors.
sv_ignoregrenaderadio "0" // Turn off Fire in the hole messages
sv_kick_players_with_cooldown 0 // (0: do not kick; 1: kick Untrusted players; 2: kick players with any cooldown)
sv_kick_ban_duration "0" // How long should a kick ban from the server should last (in minutes)
sv_lan "0" // Server is a lan server ( no heartbeat, no authentication, no non-class C addresses )
sv_log_onefile "0" // Log server information to only one file.
sv_logbans "1" // Log server bans in the server logs.
sv_logecho "1" // Echo log information to the console.
sv_logfile "1" // Log server information in the log file.
sv_logflush "0" // Flush the log file to disk on each write (slow).
sv_logsdir "logfiles" // Folder in the game directory where server logs will be stored.
sv_maxrate "0" // min. 0.000000 max. 30000.000000 replicated Max bandwidth rate allowed on server, 0 == unlimited
正如您在上面看到的,第 1 列和第 2 列彼此靠近,第 3 列从第 70 列开始整齐地向右。
您能否尝试使用显示的示例进行以下、编写和测试。
awk '
FNR==NR{
first_len=(first_len>length()?first_len:length())
second_len=(second_len>length()?second_len:length())
next
}
{
val=""
match([=10=],/\/\/.*/)
val=substr([=10=],RSTART,RLENGTH)
[=10=]=substr([=10=],1,RSTART-1)
printf("%-"first_len"s\t\t%-"second_len"s\t\t%s\n",,,val)
}
' Input_file Input_file
解释:为以上添加详细解释。
awk ' ##Starting awk program from here.
FNR==NR{ ##Checking condition when Input_file is being read first time.
first_len=(first_len>length()?first_len:length()) ##Creating first_len which has length of 1st field.
second_len=(second_len>length()?second_len:length()) ##Creating second_len which has length of 2nd field.
next ##next will skip all further statements from here.
}
{
val="" ##Nullifying next statements from here.
match([=11=],/\/\/.*/) ##Using match function to match from // to till end of line.
val=substr([=11=],RSTART,RLENGTH) ##Creating val which has sub string of matched regex.
[=11=]=substr([=11=],1,RSTART-1) ##Keeping everything before matched regex in current line.
printf("%-"first_len"s\t\t%-"second_len"s\t\t%s\n",,,val) ##Printing spaces appropriately after fields to even the spaces.
}
' Input_file Input_file ##Mentioning Input_file name here.
注意: 我在输出中放置了 2 个选项卡,以防您需要单个选项卡(每列的对齐方式已经固定)然后在上面的 printf 语句中从 \t\t
更改为 \t
。
使用 GNU awk
和 column
,前提是您知道输入中不能出现的某些字符,例如 4
。此外,它假定前两列不能有 space 字符,正如从问题中显示的示例输入中推断的那样。
awk -v FPAT='//.*$|[^ ]+' -v OFS='4' '{=} 1' ip.txt | column -s$'4' -t
- 此处
FPAT
用于定义字段内容的模式。//.*$
将获取从//
到行尾的所有内容。[^ ]+
定义前两个字段的字段内容。 -v OFS='4'
将输入中不存在的一些字符设置为输出字段分隔符{=} 1
重新构建[=20=]
的内容并打印column -s$'4' -t
将根据4
字段分隔符 整齐地对齐输出
$ cat tst.awk
NR==FNR {
wid = (length() > wid ? length() : wid)
next
}
NF {
hd = sprintf("%-*s%3s%s", wid, , "", )
tl = ( (s=index([=10=],"//")) ? substr([=10=],s) : "")
[=10=] = sprintf("%-69s%s", hd, tl)
}
{ print }
$ awk -f tst.awk file file
mp_death_drop_defuser "1" // Drop defuser on player death
mp_death_drop_gun "1" // Which gun to drop on player death: 0=none, 1=best, 2=current or best
mp_friendlyfire "1" // Allows team members to injure other members of their team
mp_halftime "1" // Determines whether or not the match has a team-swapping halftime event.
mp_overtime_startmoney "16000"
mp_playercashawards "1" // Players can earn money by performing in-game actions
sv_alltalk "0" // Players can hear all other players' voice communication, no team restrictions
sv_cheats "0" // Allow cheats on server
sv_forcepreload "0" // Force server side preloading.
原答案:
这将按照您的要求从第 3 列的字符位置 60 开始:
awk '
s = index([=12=],"//") {
hd = substr([=12=],1,s-1)
sub(/[[:space:]]+$/,"",hd)
[=12=] = sprintf("%-59s%s",hd,substr([=12=],s))
}
1' file
例如鉴于此输入:
$ cat file
mp_death_drop_defuser "1" // Drop defuser on player death
mp_death_drop_gun "1" // Which gun to drop on player death: 0=none, 1=best, 2=current or best
mp_friendlyfire "1" // Allows team members to injure other members of their team
mp_halftime "1" // Determines whether or not the match has a team-swapping halftime event.
mp_overtime_startmoney "16000"
mp_playercashawards "1" // Players can earn money by performing in-game actions
sv_alltalk "0" // Players can hear all other players' voice communication, no team restrictions
sv_cheats "0" // Allow cheats on server
sv_forcepreload "0" // Force server side preloading.
$ awk 's=index([=14=],"//"){hd=substr([=14=],1,s-1); sub(/[[:space:]]+$/,"",hd); [=14=]=sprintf("%-59s%s",hd,substr([=14=],s))} 1' file
mp_death_drop_defuser "1" // Drop defuser on player death
mp_death_drop_gun "1" // Which gun to drop on player death: 0=none, 1=best, 2=current or best
mp_friendlyfire "1" // Allows team members to injure other members of their team
mp_halftime "1" // Determines whether or not the match has a team-swapping halftime event.
mp_overtime_startmoney "16000"
mp_playercashawards "1" // Players can earn money by performing in-game actions
sv_alltalk "0" // Players can hear all other players' voice communication, no team restrictions
sv_cheats "0" // Allow cheats on server
sv_forcepreload "0" // Force server side preloading.
为了获得漂亮的输出,我会首先尝试使用分隔符重新格式化您的输入以满足 column -t
。例如,您可以使用 sed 仅对前两列进行模式匹配,并在适当的地方插入一对 tab-characters:
sed 's%^\([^ ]*\) *\([^ ]*\) *%\t\t%' config.txt | column -Lts$'\t'
不过,要获得更强大的解决方案,请考虑使用 python and the str.split() 函数。这可以限制返回的元素数量(使用 'maxsplit' arg)使得再次加入 tab-character 变得非常简单。将以下内容放入名为 format_config.py
的文件中:
import sys
for line in sys.stdin:
sys.stdout.write('\t'.join(line.strip().split(None, 2)) + '\n')
然后:
python format_config.py < config.txt | column -Lts$'\t'